I have a Python script that reads through a text csv file and creates a playlist file. However I can only do one at a time, like:
python playlist.py foo.csv foolist.txt
However, I have a directory of files that need to be made into a playlist, with different names, and sometimes a different number of files.
So far I have looked at creating a txt file with a list of all the names of the file in the directory, then loop through each line of that, however I know there must be an easier way to do it.
Common Way Open your command line or terminal. Navigate to the directory where your Python script lies. Run the script with the python3 script_name.py command (The keyword may change to python according to your configuration). Done.
We can use sys. path to add the path of the new different folder (the folder from where we want to import the modules) to the system path so that Python can also look for the module in that directory if it doesn't find the module in its current directory.
Finally, double-click on the batch file in order to run the Python script. You may also want to check the following source that contains additional guides about batch scripts.
The fastest and easiest way to run all Python files in a directory is to use loops. You can use bash to do this for you. For example, create a new file called run_all_py.sh and write the following in it: You could also use xargs to parallely execute these files (Only available on UNIX).
On recent versions of Windows, it is possible to run Python scripts by simply entering the name of the file containing the code at the command prompt: C:\devspace> hello.py Hello World!
When you try to run Python scripts, a multi-step process begins. In this process the interpreter will: This bytecode is a translation of the code into a lower-level language that’s platform-independent.
Or do you want them all in the same file? Just use a for loop with the asterisk glob, making sure you quote things appropriately for spaces in filenames Is it a single directory, or nested? For nested, you can use os.walk (topdir) to get all the files and dirs recursively within a directory.
for f in *.csv; do
python playlist.py "$f" "${f%.csv}list.txt"
done
Will that do the trick? This will put foo.csv in foolist.txt and abc.csv in abclist.txt.
Or do you want them all in the same file?
Just use a for loop with the asterisk glob, making sure you quote things appropriately for spaces in filenames
for file in *.csv; do
python playlist.py "$file" >> outputfile.txt;
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With