0

I am new to VSCODE. I opened a text file via vscode and entered some details. Where can I find the file?

f=open('story.txt','w')
f.write('my name is')
f.write('my age is')
f.close()
f=open('story.txt', 'r')
print(f.readline())
f.close()

this is the output

However I cannot find 'story.txt' in file explorer. I used another text editor and then error came as file not found. but when i reopened the file in vs code I was getting a proper output.

  • 3
    In your working directory. Top candidates: The folder of the code file and your users home folder. – Klaus D. Jun 29 '21 at 11:34
  • Sorry I don't get you. But I tried this: I created a text file and then ran the program and the text file was updated. So I need to create a text file and then run the program? In thonny the file was automatically created when I used to run the program. Is it not the case with VSCode? – Young Coder Jun 29 '21 at 11:45

3 Answers3

0

When you run python script it will execute from the current working directory by defaut.

If you want to be sure where your file will be, you may pass the complete file path instead of file name only (eg: C:\\filepath\\filename.txt) or you can move to the desired directory before read/write with os.chdir(filepath)

If you don't know where the script is running you can use os.getcwd() to get this directory from your code

import os

print(os.getcwd()) #will show the current working directory

os.chdir("c:\\") #will move to C:\ directory

f=open('story.txt','w')
f.write('my name is')
f.write('my age is')

f=open('story.txt', 'r')
print(f.readline())
f.close()
A. STEFANI
  • 6,707
  • 1
  • 23
  • 48
0

From the screenshot you supplied it looks like you are running the script from C:\Users\<YourName>, then this is where your story.txt file will be.

To specify another location you need to supply the open() method with a full path

Also, it's best practice to close the file before opening it again for reading. also, you might want to use a context manager to help you with this

If you want your file saved in the directory of your script, you can use os and __file__ to locate your script's directory and use that.

import os
my_dir = os.path.dirname(__file__)
new_path = os.path.join(my_dir, 'story.txt')
print(new_path)

os documentation

__file__ explained

Almog-at-Nailo
  • 1,152
  • 1
  • 4
  • 20
  • Thanks.. This helped. Can I automatically change the destination for the file to be created? I want it to be created where the python file is and NOT C:\Users. @AlmogAtNailo – Young Coder Jun 29 '21 at 12:01
  • I had not opened the folder. I just opened the file to run it. Can I not get the terminal to run where the program is saved? – Young Coder Jun 29 '21 at 13:05
  • Not sure what you mean. If you don't want to use an absolute path in the code (which is advised but not necessary), then yes, you need to change directory in the terminal. – Almog-at-Nailo Jun 29 '21 at 13:56
-1

When you create a file with Python, the file will be create in the same folder as your script. So, if your folder all_python_scripts contains your script, your file story.txt will be created in this folder. Try to search your file in the script's folder.

LittleCoder
  • 391
  • 1
  • 13
  • Yea. That's what I thought. But it didn't happen. Instead I tried this: I created a text file and then ran the program and the text file was updated. So I need to create a text file and then run the program? – Young Coder Jun 29 '21 at 11:55
  • I've tried your code, and for me it's working, and my answer is good. Do you run your code from its folder ? – LittleCoder Jun 29 '21 at 12:23
  • No. I just opened a file from the folder. – Young Coder Jun 29 '21 at 12:52
  • Also I had a couple of my programs in the D drive itself. and when I open the D drive as a folder itself I got this error "ERROR: EPERM: operation not permitted, stat 'd:\Config.Msi" – Young Coder Jun 29 '21 at 12:54
  • So do I need to store my programs in a folder and then open that folder for file handling? – Young Coder Jun 29 '21 at 12:58
  • I think yes. I put all my python files in the same folder. – LittleCoder Jun 29 '21 at 14:26