Get a list of file names in python, and execute a script to execute the command for each file
I'm trying to make it.
After getting a file list with glob, I was thinking about executing a command with subprocess, but it didn't work.
The file has been acquired, and I want to do something like ls "acquired file name" in subprocess, but an error occurs.
I don't really want to hit ls, but I want to do the exact same thing as processing.
Applicable source code
['test.txt', 'test11.txt', 'conan.txt', 'hero.txt'] conan.txt ex.py hero.txt test.txt test11.txt Traceback (most recent call last): File "ex.py", line 9, in<module> subprocess.call (["ls" + l]) File "/root/.pyenv/versions/3.6.6/lib/python3.6/subprocess.py", line 267, in call with Popen (* popenargs, ** kwargs) as p: File "/root/.pyenv/versions/3.6.6/lib/python3.6/subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "/root/.pyenv/versions/3.6.6/lib/python3.6/subprocess.py", line 1344, in _execute_child raise child_exception_type (errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: 'ls test.txt': 'ls test.txt'
import glob, re, subprocess ex_file = glob.glob ("*. txt") print (ex_file) subprocess.call (["ls"]) for l in ex_file: subprocess.call (["ls" + l])
Answer # 1
Shell command execution in Python
Answer # 2
This is an error because you are trying to execute a command named
subprocess.call (["ls", l]) Or subprocess.call ("ls" + l, shell = True)
- about shell command execution and return value from python
- how to specify a text file after a python file at the windows command prompt
- i want to adjust the execution result using the while statement in python as expected
- use python setitimer to keep the execution interval constant
- python 3x - button execution action in tkinter when executing code
- python 3x - i want to divide the process when it is err as a result of command execution in python3 subprocess
- python - please tell me the solution of gunicorn: command not found when deploying heroku (crying)
- python execution error
- questions about the run command in python
- python - discord bot command cannot be started
- python - delete bom on the command line
- python - elements cannot be acquired after screen transition in selenium
- python 3x - about return code of python3 subprocess
- python - when trying to compress a file in a folder with a zip file with the file name acquired by oswalk, if there are subfolde
- command does not start in python discordbot
- error in python, subprocess
- django-admin command doesn't work in python django
- python - raspberry pi command
- how to press excel macro execution button in python
- python 3x - executing commands in python subprocess