Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a python script on all the files in a directory

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.

like image 384
S1syphus Avatar asked Apr 09 '10 16:04

S1syphus


People also ask

How do I run a Python script from a folder?

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.

How do I run a Python file in another directory?

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.

Can a batch file run a Python script?

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.

How do I run all Python files in a directory?

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).

How do I run a python script on Windows?

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!

What happens when you run Python scripts?

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.

How to get all files and dirs in a directory?

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.


2 Answers

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?

like image 169
falstro Avatar answered Oct 03 '22 03:10

falstro


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
like image 23
Daniel DiPaolo Avatar answered Oct 03 '22 02:10

Daniel DiPaolo