0
import os root_dir = "D:\extention" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".py"): print("File Name: ", f) print(f"Path: ", os.path.join(folder, f)) dir_path=(f"Path: ", os.path.join(folder, f)) 

with this code i can find py file in subfolder but cant run the file

I try to use os.system but get error.

I have very little knowledge of python can anyone help me.

3
  • Missing os.system in your example. And what is the error message? Commented Jan 9, 2023 at 23:01
  • PS D:\extention> & "C:/Users/My Pc/AppData/Local/Programs/Python/Python310/python.exe" d:/extention/open.py File Name: webp.py Path: D:\extention\1\webp.py Traceback (most recent call last): File "d:\extention\open.py", line 12, in <module> os.system(dir_path) TypeError: system() argument 'command' must be str, not tuple Commented Jan 9, 2023 at 23:05
  • Do not post such error messages in the comments, it is horrible to read, please edit your question. Commented Jan 9, 2023 at 23:14

2 Answers 2

2

Your dir_path is a tuple not a string. Leave out the round brackets.

import os root_dir = "D:\extention" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".py"): print(f"File Name: {f}") dir_path=os.path.join(folder, f) print(f"Path: {dir_path}") os.system(dir_path) 
Sign up to request clarification or add additional context in comments.

3 Comments

File Name: webp.py Path: D:\extention\1\webp.py Traceback (most recent call last): File "d:\extention\open.py", line 11, in <module> os.system(dir_path) TypeError: system() argument 'command' must be str, not tuple
I edited my answer, please try again with the new code.
subprocess.run provides more control over the execution of the command, such as the ability to specify the input, output, and error streams, and to set timeouts. It also allows you to run any command-line program, not just system commands and it is the recommended way to run external commands from Python
1

This will execute the .py file specified by dir_path:

import subprocess subprocess.run(["python", dir_path]) 

If you need to capture the output, you could use this:

import subprocess process = subprocess.Popen(["python", dir_path], stdout=subprocess.PIPE) output, error = process.communicate() print(output) 

You only need to integrate one of the alternatives to your code, like this:

import os import subprocess root_dir = "D:\extention" for folder, subfolders, files in os.walk(root_dir): if folder != root_dir: for f in files: if f.endswith(".py"): print("File Name: ", f) dir_path = os.path.join(folder, f) print("Path: ", dir_path) subprocess.run(["python", dir_path]) 

2 Comments

File "C:\Users\My Pc\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 1375, in _execute_child args = list2cmdline(args) File "C:\Users\My Pc\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 561, in list2cmdline for arg in map(os.fsdecode, seq): File "C:\Users\My Pc\AppData\Local\Programs\Python\Python310\lib\os.py", line 822, in fsdecode filename = fspath(filename) # Does type-checking of filename. TypeError: expected str, bytes or os.PathLike object, not tuple
you are not executing my code, because if you add a print(type(dir_path)) before the last line you will see that in my example the dir_path is str try again an let me know :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.