Python Forum

Full Version: Problem creating an archive
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone, first of all, sorry if my english is quite poor.

I'm new into programming and python, and today, I found my first newbie problem that I can't solve, even after I googled it a lot.


I'm trying to create a text file that doesn´t exist in the working directory, with this:

file = open('file.txt', 'w')
Or with this one:

file = open('file.txt', 'w+')
In every case I get the same error:
FileNotFoundError: [Errno 2] No such file or directory: 'file.txt'


I understood that, if I use that code, the program will open the file, and if it doesn't exists, it will create it.
I've tried a lot of things, like reinstalling VS Code, Python, checked the paths, checked the directory permissions, but nothing works, and I can't find the solution.

Thank you in advance!
Is that the only line in your program, or is there more?

Can you try creating a program with just that one single line and reproduce it? What OS are you running this on? Windows?
You should post your full code. Is the directory valid? Try using an absolute link? Is this a .py file or a py2exe file?
(May-10-2020, 01:16 AM)mwd1993 Wrote: [ -> ]You should post your full code. Is the directory valid? Try using an absolute link? Is this a .py file or a py2exe file?

I'm using VS Code on Windows.
The proyect is in .py
I've created a folder named "Proyects" in my documents.
I'm working inside that folder, and when I check the current directory, it is the same one.

The tutorial I am watching, use this:

def app():

    file = open('file.txt', 'w')

app()
After that, the guy explains that "file.txt" was created because it didn't exists, and it is now on the activity bar together with the .py files. I tried some things about absolute path I read in other forums, but I really don't know if that's the problem, or how to do it correctly