Try using the pathlib library. It should take care of the formatting for you. Assuming the folder with the file is somewhere in your own home directory, this code should do the trick:
import os from pathlib import Path generator = Path.home().glob('**/US_Accidents_Dec19.csv') wd = list(generator) os.chdir(wd)
The pathlib library can work with paths independent of your operating system.
Path.home() finds your user directory, e.g. '\Users\MyAccount' or 'C:\Users\MyAccount'.
glob() function can find files based on patterns for you.
**/ means search all subfolders.
If you want to search not only your users' directory, but all users directories, simply do
If the file you are looking for can be found in the same folder or in subfolders relative to your script, try using
Path(__file__).resolve() to get the complete path to your file and then use glob on this.
generator = Path(__file__).resolve().glob('**/US_Accidents_Dec19.csv') wd = list(generator) os.chdir(wd)
glob() function returns a generator object. A generator object can be turned into a list with
list(obj) or you can iterate over it with a for-loop as if it was a list.