簡體   English   中英

在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 文件相關。 如果您無法確定工作目錄和正確的文件相對路徑,請使用絕對路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM