Home > OS >  How to increment and execute as python script
How to increment and execute as python script

Time:12-05

I am looking for an efficient way to create a python script that mass produce hundreds of folders that increment each time. For example, folder 1 folder 2 etc.

I've come up with a sample code

x =3 

for x in range (10):
    x  = 1
    print(f'{"episode "}{x}')

I tried running with cat increment.py | xargs mkdir but I got this instead: wrong folder output names

CodePudding user response:

import os

# Set the starting number for the folders
x = 3

# Loop through the range of numbers you want to create folders for
for x in range(10):
    # Increment the number
    x  = 1
    
    # Use the os.mkdir() function to create a folder with the current number
    # as the name of the folder
    os.mkdir(f'folder {x}')

CodePudding user response:

# define the starting number for the folders
x = 3

# loop through the range of numbers you want
for i in range(10):
    # increment the number
    x  = 1

    # create the folder name
    folder_name = f'episode {x}'

    # output the folder name to stdout
    print(folder_name)

This script will output the names of the folders to be created to stdout, one per line. To create the folders, you can use the xargs command like this:

python script.py | xargs mkdir

This will run the script, which will output the names of the folders to be created to stdout. The xargs command will read those names from stdout and pass them as arguments to the mkdir command, which will create the folders.

  • Related