"No such file or directory" 在 python 中使用 "cat" 时出错
"No such file or directory" error when using "cat" in python
当我使用
cat folder1/folder2/*_in.txt>>output.txt
直接在终端中,它工作正常。
但是当我从 python 进程内部调用时,它显示错误 "No such file or directory"
:
command = "cat "+path+"*_in.txt >> " + output_variable
print(command) # print exactly the same.
os.system(command) # error : cat: folder1/folder2/*_in.txt: No such file or directory
当没有文件匹配模式时会发生这种情况:
$ python -c 'import os; os.system("cat *.txt")'
cat: '*.txt': No such file or directory
$ echo 'Hello World' > myfile.txt
$ python -c 'import os; os.system("cat *.txt")'
Hello World
请注意,文件是相对于进程的当前工作目录 (os.getcwd()
) 而匹配的,而不是相对于 Python 文件。如果您无法确定工作目录和正确的文件相对路径,请使用绝对路径。
当我使用
cat folder1/folder2/*_in.txt>>output.txt
直接在终端中,它工作正常。
但是当我从 python 进程内部调用时,它显示错误 "No such file or directory"
:
command = "cat "+path+"*_in.txt >> " + output_variable
print(command) # print exactly the same.
os.system(command) # error : cat: folder1/folder2/*_in.txt: No such file or directory
当没有文件匹配模式时会发生这种情况:
$ python -c 'import os; os.system("cat *.txt")'
cat: '*.txt': No such file or directory
$ echo 'Hello World' > myfile.txt
$ python -c 'import os; os.system("cat *.txt")'
Hello World
请注意,文件是相对于进程的当前工作目录 (os.getcwd()
) 而匹配的,而不是相对于 Python 文件。如果您无法确定工作目录和正确的文件相对路径,请使用绝对路径。