简体   繁体   中英

"No such file or directory" error when using "cat" in python

When I use

cat folder1/folder2/*_in.txt>>output.txt 

directly in the terminal, it works fine.

But when I call from inside the python process, it shows an error "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

This happens when there are no files matching the pattern:

$ 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

Be aware that files are matched relative to the process's current working directory ( os.getcwd() ), and not relative to the Python file. If you can't figure out the working directory and correct relative path to your files, use absolute paths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM