1

I am opening a new file (which didn't exist until now) using this line of code:

file_number = input("What number player are you? ")
with open(os.path.join(playergold, "gold.%s.txt" % file_number), "w") as g:

#playergold is the directory 'PlayerFiles/PlayerItems'

The computer can use the files once, but not read the files, or write in them afterwards, and I cannot find them anywhere in the file view screen. Even after the program successfully ends, they are none-existent. Is this something repl.it does, or are they loading wrong, or something else I don't know? Please help! Anything at all is helpful!

This is what my screen looks like: enter image description here

On the left, if you enter in a new number, a new file is supposed to be put inside the PlayerGold folder. (The other error I am currently experiencing is not a part of the question.)

Frasher Gray
  • 222
  • 2
  • 14
  • 1
    Try to open file in `binary` mode for witing and write binary or Unicode string to tackle `io.UnsupportedOperation` – sam Dec 07 '19 at 17:11
  • 1
    Nvm, I forgot to change the mode from `"w"` to `"r"` when reading. Looks like it _is_ possible to create the file, write to it and then read from it, but it doesn't show up in the file browser anyway... – ForceBru Dec 07 '19 at 17:14
  • @Sampath , my writing code was just wrong. It said `'r'` instead of `'w'`. – Frasher Gray Dec 07 '19 at 20:00

1 Answers1

0

Welcome to Stackoverflow! Let me try to answer this,

  1. You are opening a new file in write mode. So basically, you are creating it. This is similar to opening a VI editor. You can open VI, write something and close it without saving it!

  2. Since you are using both context manager and saving file pointer as g, try to print it. Here is one example of expected output for g

>>> fp = open('/tmp/sam123', 'w')
>>> fp
<_io.TextIOWrapper name='/tmp/sam123' mode='w' encoding='UTF-8'>
  1. (Optional) If possible try to segregate your code to do a os.path.join and open in two different steps. This helps in debugging.
Frasher Gray
  • 222
  • 2
  • 14
sam
  • 1,819
  • 1
  • 18
  • 30
  • How do I permanently make a file with code then? Because that is the objective. – Frasher Gray Dec 07 '19 at 19:50
  • Also, why would I want to print it? – Frasher Gray Dec 07 '19 at 22:48
  • @FrasherGray - I am not completely sure of how/what went wrong at your end, so I had only some potentials guesswork solutions. So is why print. If you have successfully come out f context manager('with'), your file should be created by then. – sam Dec 10 '19 at 10:55
  • Oh I see, @Sampath. If it prints, it exists, it it doesn't, something when wrong? – Frasher Gray Dec 10 '19 at 12:36
  • I am going to try downloading the files and running it on my computer. Because I ran the code on repl.it and it printed the correct directories and file, but it still didn't show up. – Frasher Gray Dec 10 '19 at 12:40