im trying to pass the "file" as variable to the function "processEntry". But if i pass the variable as an argument, my program doesn't find the file.
import pandas as pd
import os, sys
PATH = os.path.dirname(os.path.abspath(__file__))
def processEntry(file):
print("Processing...")
try:
pathToFile = os.path.join(PATH, file)
df = pd.read_excel(pathToFile)
print("Done!")
return df
except FileNotFoundError:
print('File not found')
if __name__ == '__main__':
file = 'test.xlsx'
processEntry(file)
Result:
Processing... File not found
The problem isn't the path or the file, because exist: I create a variable inside the function to test it with the exact same value as "file", and the program find my file as it should be. (Same code, but using "internalVariableFile" inside the function with exact the same string as "file"):
import pandas as pd
import os, sys
PATH = os.path.dirname(os.path.abspath(__file__))
def processEntry(file):
print("Processing...")
internalVariableFile = 'test.xlsx'
try:
pathToFile = os.path.join(PATH, internalVariableFile)
df = pd.read_excel(pathToFile)
print("Done!")
return df
except FileNotFoundError:
print('File not found')
if __name__ == '__main__':
file = 'test.xlsx'
processEntry(file)
Result:
Processing... Done!
Someone can help me? I want to pass the file as an argument to the function, and i dont know why the first code didn't work. Thanks!