Python 在绝对路径中找不到文件

Python 在绝对路径中找不到文件,python,python-3.x,Python,Python 3.x,我有一个脚本,可以创建一个文件并通过os.rename移动它,该文件是在脚本运行时生成的,但是当我打开文件时 os.rename(str(PATH_TO_PROGRAM)+str("Outputs/important_output.txt"), str(args['output_dir'])+ str("important_output.txt")) with open(str(args['output_dir'])+str("importan

我有一个脚本,可以创建一个文件并通过
os.rename
移动它,该文件是在脚本运行时生成的,但是当我打开文件时

os.rename(str(PATH_TO_PROGRAM)+str("Outputs/important_output.txt"), str(args['output_dir'])+ str("important_output.txt"))

with open(str(args['output_dir'])+str("important_output.txt), "r") as infile:
     do stuff
我得到以下错误:

  File "my_script.py", line 2863, in <module>
    with open(str(args['output_dir'])+str("important_output.txt"), "r") as infile:

FileNotFoundError: [Errno 2] No such file or directory: '/mnt/c/Users/jaysp/Desktop/new/important_output.txt' 
文件“my_script.py”,第2863行,在
以open(str(args['output_dir'])+str(“important_output.txt”),“r”)作为填充:
FileNotFoundError:[Errno 2]没有这样的文件或目录:'/mnt/c/Users/jaysp/Desktop/new/important_output.txt'

我不知道为什么找不到该文件。它是在脚本运行时生成的。我在我复制到的目录中看到它。如果有帮助,我将从Windows Subsystem Linux运行脚本,并在VSCode中编辑代码。

尝试使用os.path.join来加入路径

input = os.path.join(PATH_TO_PROGRAM, "Outputs/important_output.txt")
output = os.path.join(args['output_dir'], "important_output.txt")
os.rename(input, output)

with open(output, 'r') as infile:
  ...

很明显/mnt/c/Users/jaysp/Desktop/new/important_output.txt不存在,或者您对该目录没有权限,请键入ls-lh/mnt/c/Users/jaysp/Desktop/new并检查您是否具有执行权限(+x)@bug先生权限是-rwxrwxtype file/mnt/c/Users/jaysp/Desktop/new/important_output.txt,并检查您是否有typo@Mr.bug我直接从文件NotFoundError中复制,得到的结果是:/mnt/c/Users/jaysp/Desktop/new/important\u output.txt:ASCII textUpdate:我在另一台计算机上使用了该脚本,它工作正常。是什么导致了这个错误?