Jump to content

Help with Deleting A Line In Python

TheSnowman37
Go to solution Solved by Hans Christian | Teri,

What exactly is it that you want to do? Print the file and select lines to delete? Just delete line 2 to 4? Either way your problem can probably be solved by loading the lines of text into an array, removing the lines you don't want to keep, and overwriting the file with what lines are left in the array.

I have been looking thourgh the internet for close to an hour and a half, and have yet to find some that A) Works, B) Is Simple to understand so that I can replicate it, and C) Is actually what I'm looking for. All I want to do is just simply remove a line. I'm not looking to remove a specific line with specific text, just a line in a .txt file. Ex:

(The Numbers Just represent the line #, not written to file)

 

1: A

2: C

3: D

4: T

5: X

 

delete line 2-4

 

1: A

2: X

 

The lines won't always have the same text, so I can't just delete that. I'm not going the share the real code as it is all just opening the document, printing it, and closing it. Reply if you need anymore information.

-

Link to comment
Share on other sites

Link to post
Share on other sites

What exactly is it that you want to do? Print the file and select lines to delete? Just delete line 2 to 4? Either way your problem can probably be solved by loading the lines of text into an array, removing the lines you don't want to keep, and overwriting the file with what lines are left in the array.

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, Hans Christian | Teri said:

What exactly is it that you want to do? Print the file and select lines to delete? Just delete line 2 to 4? Either way your problem can probably be solved by loading the lines of text into an array, removing the lines you don't want to keep, and overwriting the file with what lines are left in the array.

That worked, thank you. I glad that I could find a good source of information.

-

Link to comment
Share on other sites

Link to post
Share on other sites

32 minutes ago, TheSnowman37 said:

-snip-

This might help you. 

https://stackoverflow.com/questions/4710067/using-python-for-deleting-a-specific-line-in-a-file

 

Intel® Core™ i7-12700 | GIGABYTE B660 AORUS MASTER DDR4 | Gigabyte Radeon™ RX 6650 XT Gaming OC | 32GB Corsair Vengeance® RGB Pro SL DDR4 | Samsung 990 Pro 1TB | WD Green 1.5TB | Windows 11 Pro | NZXT H510 Flow White
Sony MDR-V250 | GNT-500 | Logitech G610 Orion Brown | Logitech G402 | Samsung C27JG5 | ASUS ProArt PA238QR
iPhone 12 Mini (iOS 17.2.1) | iPhone XR (iOS 17.2.1) | iPad Mini (iOS 9.3.5) | KZ AZ09 Pro x KZ ZSN Pro X | Sennheiser HD450bt
Intel® Core™ i7-1265U | Kioxia KBG50ZNV512G | 16GB DDR4 | Windows 11 Enterprise | HP EliteBook 650 G9
Intel® Core™ i5-8520U | WD Blue M.2 250GB | 1TB Seagate FireCuda | 16GB DDR4 | Windows 11 Home | ASUS Vivobook 15 
Intel® Core™ i7-3520M | GT 630M | 16 GB Corsair Vengeance® DDR3 |
Samsung 850 EVO 250GB | macOS Catalina | Lenovo IdeaPad P580

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×