Python file delete line first




















Learn how your comment data is processed. Related Posts. Leave a Comment Cancel Reply Your email address will not be published. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site.

Not consenting or withdrawing consent, may adversely affect certain features and functions. The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access that is used exclusively for statistical purposes. Ask Question. Asked 8 years, 1 month ago. Active 8 months ago. Viewed k times. I have been searching online, but have not found any good solution. Could anyone please help me with that? Maybe python isn't the weapon of choice for this. Hyperboreus If the user wants to use Python, then so be it.

There are quick ways to do this in Python as well as any other language. I just wanted to point out another way, in case OP was too fixed on python which I do not know. You know: "If the only tool you know is a hammer, all problems look like nails". I did the same using "sed command" invoked on the shell from python script but my code reviewer didn't want to spawn a new process for such a simple thing Both have pros and cons, like in file. Show 2 more comments.

How to delete a specific line in a file? Ask Question. Asked 11 years ago. Active 1 year ago. Viewed k times. SourD SourD 2, 7 7 gold badges 26 26 silver badges 28 28 bronze badges. Try fileinput as described by j-f-sebastian here. It seems to allow you to work line-by-line, via a temporary file, all with a simple for syntax.

Add a comment. Active Oldest Votes. Then reopen the file in write mode and write your lines back, except for the line you want to delete: with open "yourfile. Boris 9, 7 7 gold badges 72 72 silver badges 74 74 bronze badges. Ooker: You have to open the file twice and close it in between because in the first mode it is "read-only" because you are just reading in the current lines in the file.

You then close it and re-open it in "write mode", where the file is writable and you replace the contents of the file sans the line you wanted to remove. Why does Python not allow us to do this in one line? Ooker, When you read a line, try to imagine a cursor moving along the line as it's read. Once that line has been read the cursor is now past it.

When you try to write into the file you write where the cursor currently is. By re-opening the file you reset the cursor. This task can be done opening the file only once Show 2 more comments.

Solution to this problem with only a single open: with open "target. Lother Lother 1, 1 1 gold badge 11 11 silver badges 17 17 bronze badges. This worked very well for me, as I had to use lockfile also fcntl. I couldnt find any way to use fileinput together with fcntl. It would be nice to see some side effects of this solution. I wouldn't do this. If you get an error in the for loop, you'll end up with a partially overwritten file, with duplicate lines or a line half cut off. She bought a farm that grows peaches.

There is a lovely grape orchard beyond the hills. Some changes have been made. He claims to have seen a UFO. There was little to eat on the island besides coconuts.

This time, we need to remove the shortest line in the document. We can do this by using the len method to find the length of each line.



0コメント

  • 1000 / 1000