Using python to edit text files




















Let's examine how to read the data which we have written to the file. Open a file using the open in r mode. Read data from the file using read or readline or readlines methods. Store the data in a variable. Hafeezul Kareem. Previous Page Print Page. Andrea Never transcribe your code but rather copy and paste it Show 6 more comments.

Active Oldest Votes. In your code just replace the line for line in text: with for line in text. You can examine the file pointer by embedding text. Improve this answer. Prakash Kuma Prakash Kuma 1 1 gold badge 5 5 silver badges 11 11 bronze badges. I'm not the one who downvoted you, but I did check this answer and it didn't appear to change anything. Actually it does change things, but the description of what it does is poor and it's probably not what you want.

What he is trying to say, between the cursing and the poor formatting, is that you cannot write to the end of the file while you are in the middle of reading from it. The change fixes that, but ends up appending new output to the end of the file, not rewriting the file.

I already mentioned that after reading the code i assumed that only. Also if u really wanna rewrite why don't u open the file save it in a list and after modification of the list write it back. The problem with the code was that after read the file the file pointer moved to the end and thus the rewriting the existing line is not possible unless u move the file pointer. And tripleee as far as poor formatting is concerned, u can check that i have started since yesterday, still i am trying to be as accurate as possible.

I really don't know what are the proper formatting rules and still learning. And Andrea I know u have not down voted and i was not pointing towards u, Since u don't have the required credits to downvote just like me. But the problem is with file pointer i think. Check these links in case u have not seen them.

Show 1 more comment. There are different functions for stripping from both sides, from the left, and from the right only. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to edit a text file? Ask Question. Asked 2 years, 11 months ago. Active 2 years, 11 months ago. Viewed 2k times. Does anyone know how to do this? Improve this question.

Cro Simpson2. It looks like you're just appending data to the file. Is that what you want to know, or will the "edits" involve other things like changing existing lines or deleting some of them. You need to split the lines to get the columns. I was trying to split the line, but then I got strings and I don't know how to make the first column with that, also there were problems with adding those values to new list — Cro Simpson2. The open built-in Python method doc uses normally two arguments: the file path and the mode.

You have three principal modes the most used : r , w and a. This is the argument you should use when adding lines to non empty files. You must not forget to close the file once you have finished working with it with myFile. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to open and edit an existing file in Python? Ask Question. Asked 7 years, 1 month ago. Active 3 years, 9 months ago. Viewed 23k times. Improve this question.



0コメント

  • 1000 / 1000