2

I'm passing the file name trough sys.argv with slashes not backslashes . I'm using os.path.normpath and os.path.join but when trying to open the file I get an exception no such file or directory: and the path is with double backslashes. I'm searching for the solution for hours but nothing works.

I'v tried every tutorial I could find on google and I just keep getting the same problem. I just keep getting double back slashes. I've tried also just hardcoding the path like in example.

filepath = os.path.normpath(os.path.join('D:/dir1/dir2/dir3', 'myfile.txt'))
try:
    my_file = open(filepath, 'w+')
except Exception as e:
    print('Cannot create/open file w+!\n{}'.format(e))

I've need to be able to open the file.

4
  • can you try just: with open('D:/dir1/dir2/dir3/myfile.txt', 'w') as write_file: foo() Commented Jun 13, 2019 at 7:38
  • Beware, all the folders in the path must exist for open to succeed... Commented Jun 13, 2019 at 7:39
  • Don't do >> os.path.join('D:/dir1/dir2/dir3', 'myfile.txt') DO>> os.path.join('D:', 'dir1', 'dir2','dir3','myfile.txt') Using this hardcode value is not good practice Commented Jun 13, 2019 at 7:42
  • yeah after 2 hours of trying and failing and then starting to use slashes and then normalize I didn't had the correct path when I replaced the backslashet with slashes. After now checking again it works with normpath and join. Commented Jun 13, 2019 at 7:58

3 Answers 3

2

In python scripts it works with:

file_path =  os.path.join(os.path.abspath(os.path.dirname(__file__)), 'config.ini')

But if I need to build a program with py2exe the __file__ doesn't work and I use:

file_path =  os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), 'config.ini')

Hope this helps some one.

Sign up to request clarification or add additional context in comments.

Comments

0

I would prefer to keep my files in a structured format where my main script will be in the root folder. This approach becomes more generic in a sense that if you try to run the same content on some other system with the different operating system, the path will raise issues.

Example

Project
  |-- main.py
  |-- files
       |--file1.txt
       |--file2.txt

Then you can simply access files by

with open("files/file1.txt", 'w+') as file_object:
    content = file_object.readlines() # Whatever the method

1 Comment

I don't know if this will work for me because I build a exe with py2exe and then I'm executing this exe with a DB trigger EXEC master..xp_cmdshell so the working dir is not the same as the programs. Else I wouldn't use fixed paths, but absulute paths like you.
0

Add r before the file path for windows.

guests = open(r"C:\folder\guests.txt","w")
initial_guests = ["Bob", "Andrea", "Manuel", "Polly", "Khalid"]

for i in initial_guests:
    guests.write(i + "\n")
    
guests.close()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.