#πŸ”’ please help me understand reading/writing from a txt file

37 messages Β· Page 1 of 1 (latest)

supple sedge
#

I now know how to read and write to a file but need to understand how to save the file after I'm done..

with open('C:\\Users\\K\\Documents\\Python\\Protox\\Assets\\config.txt', 'r+') as configread:
    line = configread.readlines()
    line[0] = "light_mode = 0"
    print(line[0])

with open('C:\\Users\\K\\Documents\\Python\\Protox\\Assets\\config.txt', 'r+') as configread:
    line = configread.readlines()
    print(line[0])

output is:

light_mode = 0
light_mode = 1

Meaning it will change it and then close it but when I re-open the file and try to read the line it will give me the previous line.

quiet aspenBOT
#

Hey @supple sedge!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
quiet aspenBOT
#

@supple sedge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

supple sedge
#

Can someone help me figure out how to save it?

knotty finch
#

Your issue is that you're not changing the file. You're reading the file as a list of lines and then changing one of the elements of that list - that doesn't concern the file.

supple sedge
#

so im not writing to it

knotty finch
#

You'll want to write that list back to the file, e.g. by doing configread.seek(0) and configread.write("\n".join(line))

#

hmm, actually that's not quite right, because you also need to truncate it by configread.truncate()

supple sedge
#

could you maybe do that in a for loop if you had multiple lines of things?

knotty finch
#

You only need to rewrite the file once, no matter how many edits you do.

supple sedge
#

well this txt file has the one line could you maybe show me how

#

with configread.write could you specify line your writing to?

knotty finch
# knotty finch You only need to rewrite the file once, no matter how many edits you do.

So consider:

with open('C:\\Users\\K\\Documents\\Python\\Protox\\Assets\\config.txt', 'r+') as configread:
    lines = configread.readlines()
    lines[0] = "light_mode = 0"
    configread.seek(0)
    configread.truncate()
    configread.write("\n".join(lines))

or even just do two opens, instead of carefully seeking etc:

with open('C:\\Users\\K\\Documents\\Python\\Protox\\Assets\\config.txt') as fo:
    lines = fo.readlines()
    lines[0] = "light_mode = 0"
with open('C:\\Users\\K\\Documents\\Python\\Protox\\Assets\\config.txt', "w") as fo:
    # 'w' mode truncates automatically, so we just write the new contents
    fo.write("\n".join(lines))
supple sedge
#

what is .seek doing?

knotty finch
# supple sedge with configread.write could you specify line your writing to?

Generally speaking, no. That's not how files work. Text files are internally just a sequence of characters, with linebreaks indicated by a special character. So to change a certain "line", you generally need to overwrite the entirety of the file past that point, because all of those contents need to be shifted.

supple sedge
knotty finch
#

here's an example:

# before the file is:
"a = 'yay'\nb = 'nay'"
# we change yay to 'wooow!'. afterwards the file is:
"a = 'wooow!'\nb = 'nay'"
# as you can see, the second line got shifted - so even though we "only changed a bit of the first line", we need to overwrite the entire second line
knotty finch
supple sedge
#

i dont wanna create a database just for my gui.. ;-;

knotty finch
# knotty finch this is most of the point of databases - in them, you *can* change a single row ...

in most formats, you can't. a notable exception is FWF ("fixed width files" (or "format")). the idea is that you keep each line a constant width by having padding in it. That means you can immediately tell where in the file a certain line starts (if each line is 50 bytes, then line 30 starts at byte 1500, so you can seek right there without even reading the previous lines), and can edit a specific line without touching the rest of the file, as long as you don't exceed the limit. It's not very common in the modern world, however - it's a big hassle and you can just use a database instead.

supple sedge
#

too much info brain hurts

#

so with a FWF you can change a specific line as long as limit is not exceeded?

knotty finch
#

yeah.

supple sedge
#

what would you suggest i do then gimme sec

#

my problem being everytime i open this app everything that was altered theme and button colours wise is defaulted

#

i wanna make a lil config file of sorts that saves the users choices made in the session that the app was open, Suggestions?

knotty finch
#

just use json, really

#

there's nothing wrong with rewriting the entire file to make changes. it's just a config, it's small.

supple sedge
#

whats json sorry?

#

well if i have to rewrite the entirety of it again whats ,join

knotty finch
supple sedge
#

idk if im ready for dat..

silver belfry
#

if u use pickle to it looks ugly and unreadible in the file it can save even custom Classes

#

json is only for dict and list

quiet aspenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.