I wrote this code.. However i can’t append elements at the end of line.. the output is just as this.. What changes does my code requires? Answer You can’t append to lines within a file without moving other data out of the way. If you can slurp the whole file into memory, this can be done without too much hassle
Tag: file
Why does compression output a larger zip file?
I really don’t understand about python compression because whenever I am trying to compress a folder or file I end up getting a very larger file 5.5 times bigger than the original file. Why does this happen? Is there any way I can compress a folder or a file with python and get an output that’s at most the size
How can I compare files quicker in Python?
Is there any way to make this script faster? I’m using one file to compare another file to print lines, if second column are equal. Input example (for both files): The command line below works equally for same purpose in bash: How can I improve this Python script? Answer If you store your lines in dictionaries that are keyed by
How to save a list to a file and read it as a list type?
Say I have the list score = [1,2,3,4,5] and it gets changed while my program is running. How could I save it to a file so that next time the program is run I can access the changed list as a list type? I have tried: But this results in the elements in the list being strings not integers. Answer
How to delete the first line of a text file?
I have been searching online, but have not found any good solution. Here is my text file: All I want is to delete the first line (which means using the second line as the first line, not filling first line with whitespace). Could anyone please help me with that? Answer Assuming you have enough memory to hold everything in memory:
How do I call a function from another .py file? [duplicate]
This question already has answers here: How do I import other Python files? (23 answers) Closed 6 months ago. file.py contains a function named function. How do I import it? The above gives an error: ImportError: No module named ‘file.py’; file is not a package Answer First, import function from file.py: Later, call the function using: Note that file is
Remove very last character in file
After looking all over the Internet, I’ve come to this. Let’s say I have already made a text file that reads: Hello World Well, I want to remove the very last character (in this case d) from this text file. So now the text file should look like this: Hello Worl But I have no idea how to do this.
watchdog monitoring file for changes
I have a need to watch a log file for changes. After looking through stackoverflow questions, I see people recommending watchdog. So I’m trying to test, and am not sure where to add the code for when files change: Where do I add the “got it” — in the while loop if the files have been added/changed? Answer Instead of
How to search and replace text in a file?
How do I search and replace text in a file using Python 3? Here is my code: Input file: When I search and replace ‘ram’ by ‘abcd’ in above input file, it works as a charm. But when I do it vice-versa i.e. replacing ‘abcd’ by ‘ram’, some junk characters are left at the end. Replacing ‘abcd’ by ‘ram’ Answer
Python inserting variable string as file name
I’m trying to create a file with a unique file name for every time my script runs. I am only intending to do this to every week or month. so I chose to use the date for the file name. is where I’m getting this error. it works if I use a static filename, is there an issue with the