Jump to content

Hiya, does anyone know how to read specific lines in a text file using python?

 

Here is the python file - Barcode Checker - Task 3.py

Here is the text file - barcodes.txt

 

In the barcodes.txt under the item name is the stock I want to make it so I can change the integers so if they buy 5 Titan X's it'll take off 5 from the txt but they can't take more than what's there.

Link to comment
https://linustechtips.com/topic/567138-python-help-just-some-simple-stuff/
Share on other sites

Link to post
Share on other sites

45 minutes ago, Sauron said:

np ^^

Heya, do you know how to do the same as in write in a specific line "writelines()" works kinda differently, I need it so it would totally overwrite the previous line and write it with the new data.

Link to post
Share on other sites

1 hour ago, mattonfire said:

Heya, do you know how to do the same as in write in a specific line "writelines()" works kinda differently, I need it so it would totally overwrite the previous line and write it with the new data.

I think you need a separate funcion to delete the line, then use write or writeline to write it again. Maybe this can help

 

http://www.afterhoursprogramming.com/tutorial/Python/Writing-to-Files/

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to post
Share on other sites

11 hours ago, Sauron said:

I think you need a separate funcion to delete the line, then use write or writeline to write it again. Maybe this can help

 

http://www.afterhoursprogramming.com/tutorial/Python/Writing-to-Files/

But I don't know how to writeline to a specific line, deleting the line is fine.

Link to post
Share on other sites

Try 

        with open("barcodes.txt", "w") as f:
            f.writelines(lines)

Before writing, you can modify lines with lines[0] = "this is a line" etc

 

Also that while loop is a tad messy, what's with the unnecessary try/except statement, and the super long "if again == "yes"" etc instead of formatting the input with ".lower()"? 

 

 

Ryzen 9 5950x | NH-D15 | ROG STRIX X570-F | G.Skill 32GB DDR4 | MSI Ventus RTX 3080 | WD Black SN850 1TB PCIe 4 | Samsung 850 EVO + 860 EVO 1TB | Corsair 5000D Airflow

Dell S3422DWG | Dell S2721DGF | Corsair K70 RGB Keyboard | Logitech G502 Lightspeed | ATH-R70x

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

×