file-io

Python fileinput: Keep file unchanged in case of exception

Python fileinput: Keep file unchanged in case of exception Question: I have a program that uses Python 3 fileinput to replace some text within files in-place. Some files causes UnicodeDecodeError as I expect since they are non-readable/binary files, but when the exception occurs these files are completely emptied out from their content. I can see …

Total answers: 1

How can I save file by using savetxt in the python?

How can I save file by using savetxt in the python? Question: I have lists of data such as a = [1,2,3,4,5] b = [6,7,8,9,0] c = [0,0,0,0,0] I want to save this data into a file in a form of 1 6 0 2 7 0 3 8 0 4 9 0 5 0 …

Total answers: 2

delete specific lines in txt file python

delete specific lines in txt file python Question: so I have text file file.txt e.g daemon nserver 1111 nserver 11111 nscache 65536 timeouts 1 5 30 60 180 1800 15 60 log /var/log/3proxy/log D logformat "- +_L%t.%. %N.%p %E %U %C:%c %R:%r %O %I %h %T" logformat "L%t.%. %N.%p %E %U %C:%c %R:%r %Q:%q %O …

Total answers: 1

Make entries appear in a new line when appending to a JSON file

Make entries appear in a new line when appending to a JSON file Question: I have been dealing with a little problem while programming python database. If I make new entry it will stay in the line with previous and I dont know where should I add n or how could I make it in …

Total answers: 1

Discord.py: Bot Keeps Writing Previous Values

Discord.py: Bot Keeps Writing Previous Values Question: So, I have a command that should give me an image and after that bot should ask whether user wants to keep it or not (I use thumbs-up reaction for the answer). If user reacts with ‘thumbs-up’, then bot says ‘Saved!’ and actually save the image URL to …

Total answers: 1

Read a file, modify it, write it out, and then read it again – better options?

Read a file, modify it, write it out, and then read it again – better options? Question: I have a fixed width text file that I am trying to read in using pandas.read_fwf. As noted here, this method removes leading and trailing whitespace. In order to get around that, I’d like to replace every whitespace …

Total answers: 1

Split a binary file into 4-byte sequences (python)

Split a binary file into 4-byte sequences (python) Question: So, basically, I have a 256MB file which contains "int32" numbers written as 4-byte sequences, and I have to sort them into another file. The thing I struggle with the most is how I read the file into an array of 4-byte sequences. First I thought …

Total answers: 3

How to monitor existing filenames on a FTP server in Python

How to monitor existing filenames on a FTP server in Python Question: I need to monitor continuously an FTP server where images appear regularly (every second or so), copy them on my client PC and delete them. Deleting the images on the server is optional but I need to have this option, and I would …

Total answers: 1

what is the syntax for adding newline to this list expression

what is the syntax for adding newline to this list expression Question: So I am trying to write a few different values of this list to a few specific lines of this text file essentially replacing them. I got a tip on my last question to seperate the lines in this output I need to …

Total answers: 1

value error(line 7 for name, home) when using csv library in python CS50P file i/o

value error(line 7 for name, home) when using csv library in python CS50P file i/o Question: import csv students = [] with open("stu1.csv") as file: reader = csv.reader(file) for name, home in reader: students.append({"name": name}, {"home": home}) for student in sorted(students, key =lambda student:student["name"]): print(f"{student[‘name’]} is from {student[‘home’]}") stu1.csv contains below data Harry, Number, Pivet …

Total answers: 1