Skip to content Skip to sidebar Skip to footer

Run Python Script On All Subdirectories With Specific Name

I have a python script which takes some parameters, and I want to run this script on all the sub directories starting from the one that the script is inside. The idea is I want to

Solution 1:

If you want to run this script on all the sub directories starting from the one that the script is inside, then try doing it this way instead:

import osforpath, directories, files inos.walk(os.path.dirname(os.path.realpath(__file__))):
    printpath, directories, files
    txt_files = [arbitrary_file for arbitrary_file in files if arbitrary_file[-4:].lower() == ".txt"]

    #run your python here
    txt_files = [txt_file for arbitrary_file in files if arbitrary_file[]

If your original code was this:

import sys

text_files_to_process = #Do Something with sys.argv - or whatever you're using to parse your arguments.withopen("res.txt", "w") as f:
    #do something with all the text files, and write the output to res.txt.for text_file in text_files_to_process:
        withopen(text_file) as tf:
            for line in tf:
                #whatever your text processing is
            tf.write("something")

then you just alter it to something like this:

import osforpath, directories, files inos.walk(os.path.dirname(os.path.realpath(__file__))):
    printpath, directories, files
    txt_files = [arbitrary_file for arbitrary_file in files if arbitrary_file[-4:].lower() == ".txt"]

    txt_files = [txt_file for arbitrary_file in files if arbitrary_file[]

    with open("res.txt", "w") as f:
        #do something with all the text files, andwrite the output to res.txt.
        for text_file in txt_files:
            with open(text_file) as tf:
                for line in tf:
                    #whatever your text processing is
                tf.write("something")

Post a Comment for "Run Python Script On All Subdirectories With Specific Name"