#πŸ”’ file doesnt get edited

98 messages Β· Page 1 of 1 (latest)

dire fog
#
f = open('python_testing.txt')
y = open('python_testing.txt','w')
test = 'testing'
with open('python_testing.txt','w') as file:
    file.writelines(test)
print(f.read())```
candid totemBOT
#

@dire fog

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.

slate cloak
#

what about when you open the file? because what youre reading there is the file before its edited

#

youre opening the file, editing and saving, and then not reading it back

dire fog
#

when I check the file on my files, its still not edited

#

how do I read the edited file

slate cloak
#
test = 'testing'
with open('python_testing.txt','w') as file:
    file.writelines(test)
with open('python_testing.txt') as file:
    print(file.read())
#

lets start with you actually opening them correctly

#

this should read it back to you as well

tame trench
dire fog
#

it still doesnt work

slate cloak
tame trench
dire fog
tame trench
dire fog
#

ye removed

slate cloak
uncut stone
dire fog
#

I managed to edit the file with other still earlier, but it suddenly stopped working

uncut stone
#

where are you looking for your file?

slate cloak
#

can you share the whole section of the code?

#

maybe a bad if statement thats not shown to us?

dire fog
tame trench
#

!code

candid totemBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

tame trench
#

any errors?

slate cloak
dire fog
#

it prints whats on the file

#

but doesnt change it

dire fog
tame trench
#

can we have a snippet of the terminal when you execute the code

dire fog
slate cloak
#

thats odd that its creating but not writeing. try just write() instead of writelines()

tame trench
#

i can see that yourte running the wrong code

tame trench
dire fog
#

OH

#

HOLY

#

IM SO DUMB

slate cloak
#

that would fix your issue

dire fog
#

AGGGHHH

#

um

slate cloak
#

but hey at least now you know you dont need all those opens before hand XD

tame trench
#

xD

dire fog
#

im so sorry, its like 2am right now and I dont think im functioning

slate cloak
#

happens to all of us

dire fog
#

my bad guys 🀦

#

alr, ty for the help

tame trench
#

np

slate cloak
#

and get some sleep

tame trench
#

xd

dire fog
slate cloak
#
import time
time.sleep(99999999999999999999999999999999999)
dire fog
#

LOL

slate cloak
#

i should probably also point out why its a bad idea to do open() instead of with open(). just doing open() you should always do var.close() when you get done with it. but with open() closes the file properly for you

dire fog
#

is there a more efficient way to edit the file other than constantly doing the with statements?

uncut stone
slate cloak
#

looks like i answered your question before you asked it

dire fog
slate cloak
#

id say just stick with with open

dire fog
#

like gaining xp and it saving in the file

dire fog
slate cloak
#

dont close the chat just yet and ill write up a mock save for you that you might like

uncut stone
slate cloak
#

going to intentionally smudge it though bc im not nice 😝

dire fog
uncut stone
dire fog
#

thats good, mainly looking to understand python concepts rather than copying code :p

slate cloak
#
export json
without 420-- as f:
  data = json.loads(f.read())
without 420-- as f:
  f.write(json.dumps(data,indent=2))
```i left out all the juicy bits but in between the reading and writing you just edit the data like you would normally
dire fog
#

man I need to study my imports

slate cloak
#

i think you mean exports XD (not a thing, unless it is)

dire fog
slate cloak
#

and it honestly depends on the situation if youre needing text files or database. id say if its a singleplayer kind of game where youre needing to save only character data for a single person, a json format file would be better suited than a database

dire fog
#

I was also wondering if its possible to run python code without needing to have an interpreter or anything

slate cloak
#

if you package it as an exe(unreliable but does work) or if you package it with an ebedded interpreter that just comes with your code

#

but for developing, you do need python installed no ifs or buts

dire fog
#

I saw something about pyinstaller for converting to exe, but im still confused

#

how do you package it with an embedded compiler though

slate cloak
#

thats the unreliable way, but its honestly what i do. as for the embedded thing i dont know. ive never needed it.

dire fog
#

alr

slate cloak
#

if i need an exe its normally just custom office software for a client needing specific equations and models built in so it just works out since i normally personally know the client. but it can get iffy because if your antivirus registers it as a virus it will fight you on it. and it does 9.9/10 times

#

i once even had pyinstaller refuse to package it bc they themselves saw it as malware for some reason

dire fog
#

how else can someone run python code from their end if they cant install an interpreter

slate cloak
#

you use an embedded python package. youll have to look that up though or hope someone else here know how bc i dont

dire fog
#

alr, tysm for the help

candid totemBOT
#
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.