file

Reading the last line of an empty file on python

Reading the last line of an empty file on python Question: I have this function on my code that is supposed to read a files last line, and if there is no file create one. My issue is when it creates the files and tries to read the last line it comes up as an …

Total answers: 2

how to not overwrite file in python in different os path

how to not overwrite file in python in different os path Question: so i want to avoid overwrite the file name that existed. but i don’t know how to combine the code with mycode. please help me here’s my code for write file: def filepass(f): print(f) with open (‘media/pass/’+’filepass.txt’, ‘a’) as fo: fo.write(f) fo.close() return …

Total answers: 1

How do I use a file like a memory buffer in Python?

How do I use a file like a memory buffer in Python? Question: I don’t know the correct terminology, maybe it’s called page file, but I’m not sure. I need a way to use an on-disk file as a buffer, like bytearray. It should be able to do things like a = buffer[100:200] and buffer[33] …

Total answers: 1

Error while converting String elements of List of Lists to float

Error while converting String elements of List of Lists to float Question: This is in reference to my previous question related to extracting data from .asc file and separating them while having multiple delimiters. I want to perform mathematical operations on the float elements of the list of lists generated from the above question. The …

Total answers: 2

I did it by the tutorial and it's not working

I did it by the tutorial and it's not working Question: I found some projects on youtube, and this is one of them. I’m trying with the password manager program. Here’s the link: https://www.youtube.com/watch?v=DLn3jOsNRVE And here’s my code: from cryptography.fernet import Fernet ”’ def write_key(): key = Fernet.generate_key() with open("key.key", "wb") as key_file: key_file.write(key)”’ def …

Total answers: 1

How to check if files are still created?

How to check if files are still created? Question: I would like to make a script to check whether or not files are still being created inside a folder. We can consider for our problem that there are no more files being created if let’s say for 5 sec the list of files present in …

Total answers: 2

.txt file opened in python won't itterate properly

.txt file opened in python won't iterate properly Question: The following contains abridged version of the code for a text card game I am trying to run. It should get a random string for a card from a random line in "cards.txt", and add it to a user’s collection at "user.txt" (user would be the …

Total answers: 1

How to optimize reading and cleaning file?

How to optimize reading and cleaning file? Question: I have a file, which contains strings separated by spaces, tabs and carriage return: one two three four I’m trying to remove all spaces, tabs and carriage return: def txt_cleaning(fname): with open(fname) as f: new_txt = [] fname = f.readline().strip() new_txt += [line.split() for line in f.readlines()] …

Total answers: 2

Remove text lines and strip lines with condition in python

Remove text lines and strip lines with condition in python Question: I have a text file in this format: 000000.png 712,143,810,307,0 000001.png 599,156,629,189,3 387,181,423,203,1 676,163,688,193,5 000002.png 657,190,700,223,1 000003.png 614,181,727,284,1 000004.png 280,185,344,215,1 365,184,406,205,1 I want to remove the lines that don’t have a [number1,number2,number3,number4,1] or [number1,number2,number3,number4,5] ending and also strip the text line and remove the …

Total answers: 1

How to get the real number after a string in a file

How to get the real number after a string in a file Question: I have files that contain both strings and floats. I am interested in finding the floats after a specific string. Any help in writing such a function that reads the file look for that specific string and returns the float after it …

Total answers: 3