file-io

python seek() and read() count file positions differently

python seek() and read() count file positions differently Question: I’m making a script that displays the text in a specific position of a file. However there is a discrepancy in how seek() and read() are counting. It goes like this. My text file is: 1 % 2 % ― % 4 % 5 % 6 …

Total answers: 2

How do I create a file path on a Mac?

How do I create a file path on a Mac? Question: I’m working an OS X platform with Python 3 and I’m not quite sure how to create file paths that link into directories. I know that on the Windows platform it would look something like import os path = ‘C:\Users\User\Desktop\<directory name>’ os.mkdir(path) file = …

Total answers: 2

What is os.linesep for?

What is os.linesep for? Question: Python’s os module contains a value for a platform specific line separating string, but the docs explicitly say not to use it when writing to a file: Do not use os.linesep as a line terminator when writing files opened in text mode (the default); use a single ‘n’ instead, on …

Total answers: 2

Python basics – request data from API and write to a file

Python basics – request data from API and write to a file Question: I am trying to use “requests” package and retrieve info from Github, like the Requests doc page explains: import requests r = requests.get(‘https://api.github.com/events’) And this: with open(filename, ‘wb’) as fd: for chunk in r.iter_content(chunk_size): fd.write(chunk) I have to say I don’t understand …

Total answers: 3

File read using "open()" vs "with open()"

File read using "open()" vs "with open()" Question: I know there are plenty of articles and questions answered regarding reading files in python. But still I’m wondering what made python to have multiple ways to do the same task. Simply what I want to know is, what is the performance impact of using these two …

Total answers: 1

Python, Pandas : write content of DataFrame into text File

Python, Pandas : write content of DataFrame into text File Question: I have pandas DataFrame like this X Y Z Value 0 18 55 1 70 1 18 55 2 67 2 18 57 2 75 3 18 58 1 35 4 19 54 2 70 I want to write this data to a text …

Total answers: 8

Saving response from Requests to file

Saving response from Requests to file Question: I’m using Requests to upload a PDF to an API. It is stored as “response” below. I’m trying to write that out to Excel. import requests files = {‘f’: (‘1.pdf’, open(‘1.pdf’, ‘rb’))} response = requests.post(“https://pdftables.com/api?&format=xlsx-single”,files=files) response.raise_for_status() # ensure we notice bad responses file = open(“out.xls”, “w”) file.write(response) file.close() …

Total answers: 3

How to solve "OSError: telling position disabled by next() call"

How to solve "OSError: telling position disabled by next() call" Question: I am creating a file editing system and would like to make a line based tell() function instead of a byte based one. This function would be used inside of a “with loop” with the open(file) call. This function is part of a class …

Total answers: 5

How to while loop until the end of a file in Python without checking for empty line?

How to while loop until the end of a file in Python without checking for empty line? Question: I’m writing an assignment to count the number of vowels in a file, currently in my class we have only been using code like this to check for the end of a file: vowel=0 f=open(“filename.txt”,”r”,encoding=”utf-8″ ) line=f.readline().strip() …

Total answers: 4

FileNotFoundError: [Errno 2]

FileNotFoundError: [Errno 2] Question: Synopsis: How do I read a file in Python? why must it be done this way? My problem is that I get the following error: Traceback (most recent call last): File “C:UsersTerminalDesktopwkspcfiletesting.py”, line 1, in <module> testFile=open(“test.txt”) FileNotFoundError: [Errno 2] No such file or directory: ‘test.txt’ Which originates from the following …

Total answers: 1