简体   繁体   中英

How to pass a file in sub-folder as parameter to a function in python

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

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))

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))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM