file-handling

Update same object in a JSON file without creating a new object inside it

Update same object in a JSON file without creating a new object inside it Question: I have an empty json file which I’m trying to update based on user input. Initially there is no content inside the json file. I’ve created an endpoint to take input from the user in following format via Flask app …

Total answers: 1

NameError: name '__file__' is not defined , os.path.dirname(os.path.abspath(__file__))

NameError: name '__file__' is not defined , os.path.dirname(os.path.abspath(__file__)) Question: I’m trying to use os.path.dirname(os.path.abspath(file)) for tracking my file txt but i don’t why its not working with me: import os print(os.getcwd()) # Get Working directory print(os.path.dirname(os.path.abspath(__file__))) file = open("Youness.txt") NameError: name ‘__file__’ is not defined. Asked By: Error 404 || Source Answers: There is a …

Total answers: 1

excess blank line is printing

excess blank line is printing Question: so I want to read a text file and print its all content in uppercase character. I written the code and it’s correct also. But I don’t know why the excess blank line is printing between the two line. import pickle file=open("STORY.TXT",’r’) string=file.readlines() for x in string: print(x.upper()) file.close() …

Total answers: 1

How to print the object itself instead of the location of the object python?

How to print the object itself instead of the location of the object python? Question: I am making a program that will read lines from a text file which contain the question, the answer and the points awarded which are on lines i, i+1 and i+2 respectively. The lines will be read and then saved …

Total answers: 2

For a newly created file, is there any difference between write() and append() methods?

For a newly created file, is there any difference between write() and append() methods? Question: For a newly created file, is there any difference between write() and append() methods, in Python? Asked By: Anshul Gupta || Source Answers: The write method overwrites the content in a text file while the append method appends text to …

Total answers: 3

Processing large files in chunks: inconsistent seek with readline

Processing large files in chunks: inconsistent seek with readline Question: I am trying to read and process a large file in chunks with Python. I am following this blog that proposes a very fast way of reading and processing large chunks of data spread over multiple processes. I have only slightly updated the existing code, …

Total answers: 4

.write() is only writing to output file in the last iteration of a loop

.write() is only writing to output file in the last iteration of a loop Question: I’m trying to open a bunch of files in a directory, remove some words from those files and write the output to a file in the same directory. I’m having problems when trying to write to the output file. There …

Total answers: 1

Why does my Python code print the extra characters "" when reading from a text file?

Why does my Python code print the extra characters "" when reading from a text file? Question: try: data=open(‘info.txt’) for each_line in data: try: (role,line_spoken)=each_line.split(‘:’,1) print(role,end=”) print(‘ said: ‘,end=”) print(line_spoken,end=”) except ValueError: print(each_line) data.close() except IOError: print(“File is missing”) When printing the file line by line, the code tends to add three unnecessary characters in …

Total answers: 3