So far I have code that opens a text file, manipulates it into a pandas data file, then exports to excel.
I'm sharing this code with other people, and we all have the same working directory within Spyder. All the code works fine, the only lines I want to manipulate are the opening of the file, and the exporting of the file.
with open(r'C:\Users\"my_name"\Desktop\data\file.txt', 'r') as data_file:
with open(r'file.txt', 'r') as data_file:
The answer that you are technically looking for is using
os.chdir() as follows
import os os.chdir('.', 'data') #THE REST OF THE CODE IS THE SAME with open(r'file.txt', 'r') as data_file:
A safer answer would however be
def doTheThing(fName): return os.path.join(os.getcwd(),'data',fName) with open(doTheThing('file.txt'), 'r') as data_file: