0

excerpt from my python script located at C:\Users\my_name\documents\python_projects\randomness\random.py :

some_number = 3452342
filename = str(some_number) + '.csv'

# file 3452342.csv is stored in C:\Users\my_name\documents\python_projects\randomness\history
# call a function that takes the filename as the parameter

my_func(r'history\filename')

It triggers the following error:

FileNotFoundError: [Errno 2] File b'history\filename' does not exist: b'history\filename'

what exactly is going wrong here? How can I pass the filename to my_func when it is located in a sub-folder?

thanks in advance

1
  • 1
    You are passing the path history\filename as the path while filename is a variable. Try my_func(f'history\{filename}') instead. The {} will pass the value of filename instead of the string filename. Commented Dec 23, 2019 at 2:47

3 Answers 3

1

The answer to your question what's going wrong: Python tried to open the file with the name literally "filename" in the subdirectory named "history", which is doesn't exist. You should pass the filename variable's value instead as follows:

You should use os.path.join().

import os

some_number = 3452342
filename = str(some_number) + '.csv'
workdir = "C:\Users\my_name\documents\python_projects\randomness\history"

my_func(os.path.join(workdir, filename))

Or if the file 3452342.csv placed in a subfolder (called history) of the the main script's directory, then you can use:

import os

some_number = 3452342
filename = str(some_number) + '.csv'

my_func(os.path.join("history", filename))

Alternatively you can simply use string concatenation:

import os

some_number = 3452342
filename = str(some_number) + '.csv'

my_func("history/" + filename)

Another approach using Python's format():

import os

some_number = 3452342
filename = str(some_number) + '.csv'

my_func("history/{}".format(filename))
Sign up to request clarification or add additional context in comments.

Comments

1

First try to get the current path then join the path you got with the name of your file.

import os

some_number = 3452342
filename = str(some_number) + '.csv'

path_file = os.path.join(os.getcwd(), filename)

my_func(path_file)

for more about how to work with path using python check out this. Common pathname manipulations

4 Comments

If os.getcwd() or os.path.dirname(os.path.abspath(__file__)) should be used a base path depends on the desired logic of the code.
exactly he asked to get the path in the same sub folder.
Then IMHO os.path.dirname(os.path.abspath(__file__)) should be preferred, since os.getcwd() might change during runtime, or might even start off different if the script were be imported as a module from another directory.
yep at that point you are right! the best way is to allow the user set the path because the path might change on the runtime
1

First, to be platform independent you should use os.path.join to concatenate directories.

Second, like @k88 pointed out, you need to pass the variable filename into your path, not the string 'filename'

The clean way would be:

import os

some_number = 3452342
filename = str(some_number) + '.csv'

# file 3452342.csv is stored in C:\Users\my_name\documents\python_projects\randomness\history
# call a function that takes the filename as the parameter

my_func(os.path.join('history', filename))

Relative Or Absolute Path?

If your history subfolder is a fixed subfolder of your script's directory, you should even consider to determine your target filename as an absolute path like this (see also this answer with comments):

base_dir = os.path.dirname(os.path.abspath(__file__))
my_func(os.path.join(base_dir, 'history', filename))

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.