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

KawaiKx
  • 9,558
  • 19
  • 72
  • 111
  • 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`. – k88 Dec 23 '19 at 02:47

3 Answers3

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))
ascripter
  • 5,665
  • 12
  • 45
  • 68
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))
Geeocode
  • 5,705
  • 3
  • 20
  • 34
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

Bob White
  • 733
  • 5
  • 20
  • 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. – ascripter Dec 23 '19 at 03:18
  • exactly he asked to get the path in the same sub folder. – Bob White Dec 23 '19 at 03:24
  • 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. – ascripter Dec 23 '19 at 03:32
  • 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 – Bob White Dec 23 '19 at 03:44