PATH_OLD = r' c:UsersBilalPythonTask1OlderVersionFiles'在我的。env文件中。我得到以下内容:
"r'C:\\Users\\Bilal\\Python\\Task1\\OlderVersionFiles\\'"
不是
r'C:\Users\Bilal\Python\Task1\OlderVersionFiles\'
运行以下代码:
import os
from dotenv import load_dotenv
load_dotenv()
result = os.getenv("PATH_OLD")
#Prints the correct path here
print(result)
dir_A_dict = dict()
directory_A = result
dir_A_files= [os.path.join(directory_A, x) for x in os.listdir(directory_A) if '.csv' in str(x)]
给出以下错误:
dir_A_files= [os.path.join(directory_A, x) for x in os.listdir(directory_A) if '.csv' in str(x)]
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: "r'C:\\Users\\Bilal\\Python\\Task1\\OlderVersionFiles\\'"
您正在为os.listdir
提供一个字符串,该字符串为而不是一个有效的目录名,所以它会报错。你在环境变量上太聪明了,它不应该是Python语法。简单点:
PATH_OLD = C:UsersBilalPythonTask1OlderVersionFiles