0

When I tried the following code in my IDE and in the command prompt with python test.py, my text file was still empty afterward:

with open("test.txt", "r+") as file:
    file.write("Hello World")

I was confused, because this worked before. So I tried the exact same code in the python console. And voila, my text file test.txt now contained the string Hello World

When I changed the content of the text file manually and tried to read it again with the program, the output was the string I tried to write earlier to the text file.

Is there some kind of delay when trying to write and read to and from a file when using a script in comparison to using the python console?

NAND
  • 663
  • 8
  • 22
El Fuerst
  • 73
  • 6
  • 3
    I suspect that the current working directory is different between your two scenarios, so you're dealing with two entirely different `text.txt` files, in different directories. – jasonharper Jun 01 '20 at 19:19
  • 1
    I believe the cmd is located somewhere else, so you actually created two files. – Red Jun 01 '20 at 19:19
  • @jasonharper I just opened the python console in my current working folder and tried it again. The console input altered my original test.txt as I wanted. Afterward, I tried the same with the script and it failed to write and to read. Is there something wrong with how I try to open the text file inside my script? It is still the same code. – El Fuerst Jun 01 '20 at 19:30

2 Answers2

0

I believe the cmd is located somewhere else.

You can change the location of where you are operating in the cmd with the os module:

>>> import os
>>> os.chdir(path) # replace path with the path you want to write to a file in
>>> with open('text.txt','r+') as file:
...     file.write("Hello World")
...
>>>

You can also find the current directory of your cmd by entering os.getcwd(). Or, you can just add the path of the place you want to write in the file to the file name:

>>> import os
>>> with open(os.path.join(path,'text.txt'),'r+') as file:
...     file.write("Hello World")
...
>>>
Red
  • 26,798
  • 7
  • 36
  • 58
  • @AnnZen I tried exactly this and the other recommended approach and when I tried to read the text file, the script returned the string "this is a test input", which I tried to write to the file earlier. It really seems like there are two different files, but I don't understand where the second one is supposed to be located. Also after I added in the complete path like you suggested. – El Fuerst Jun 01 '20 at 19:43
  • @AnnZen I just checked everything and I am 100% sure that I'm in the correct path with my script. It rather seems that there is a hidden copy of the text file that the script is trying to access. Do you have any further ideas? Thank you for your effort – El Fuerst Jun 01 '20 at 20:26
  • @AnnZen Yes, sure I do. How could I have opened the python script otherwise? I am at the right location, but the text file I am trying to access within the same folder doesn't seem to be the same file I can access via the python console in the same folder. The must be some kind of invisible copy, that the script uses. – El Fuerst Jun 01 '20 at 20:46
  • Try entering ```os.getcwd()``` in your cmd – Red Jun 01 '20 at 20:48
0

Try pass the absolute path of the file.

It should be like this

with open("/the/path/test.txt", "r+") as file:
    file.write("Hello World")
NAND
  • 663
  • 8
  • 22