Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Py Script for looping over files

  1. #1
    Join Date
    Mar 2007
    Location
    UK
    Beans
    141
    Distro
    Lubuntu

    Py Script for looping over files

    I am fairly new to python and I was just wondering if someone could post some code that would allow me to loop over files in a directory and perform a video conversion (using ffmpeg) one by one (so as not to chew up my CPU).
    I was trying with os.walk() and os.listdir() but my knowledge of python is pretty limited. Thanks for any help/code

  2. #2
    Join Date
    May 2007
    Beans
    244
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Py Script for looping over files

    I don't know how to do this in Python, so I probably shouldn't be answering at all... But I just thought I'd mention that, even when I do learn Python, this is probably something I'd still use Perl for. The Perl syntax for looping over all files in the current directory is:
    Code:
    #!/usr/bin/perl
    
    while(<*>)
    {
      print "$_ \n";
    }
    Then, instead of printing the filenames, you just replace that line with whatever you want to do, where $_ is the variable for the filename.

    If you're trying to run command-line programs on each file (I assume ffmpeg is something you run from the command-line?), you can use system
    Code:
    system("ffmpeg $_ ...");
    Last edited by Soybean; August 10th, 2007 at 12:46 PM. Reason: submitted early by accident

  3. #3
    Join Date
    Mar 2006
    Beans
    323
    Distro
    Ubuntu 6.10 Edgy

    Re: Py Script for looping over files

    Code:
    import glob
    
    mpgfiles = glob.glob("~/video/*.mpg")
    
    for file in mpgfiles:
      system("ffmpeg %s ...." % file)
    glob is your friend

  4. #4
    Join Date
    Mar 2007
    Location
    UK
    Beans
    141
    Distro
    Lubuntu

    Re: Py Script for looping over files

    Woo! Thanks.

    Just one last thing - each file will obviously have a different name, so the ffmpeg command will be something like:

    ffmpeg -i movie.avi convertedmovie.avi

    If I put this into the system("ffmpeg %s ...." % file) do I use %s where the file name should be?

    Thanks

  5. #5
    Join Date
    Mar 2006
    Beans
    323
    Distro
    Ubuntu 6.10 Edgy

    Re: Py Script for looping over files

    The python syntax for string formatting is
    Code:
    mystring = "I am an %s from %s" % ("alien","Mars")
    I guess what you want is something like:
    Code:
    import glob
    
    avifiles = glob.glob("~/video/*.avi")
    
    for file in avifiles:
      outfile = "converted_%s" % file  # a.avi becomes converted_a.avi
      command = "ffmpeg -i %s %s " % (file, outfile) # create command
      system(command) # Run command

  6. #6
    Join Date
    Mar 2007
    Location
    UK
    Beans
    141
    Distro
    Lubuntu

    Re: Py Script for looping over files

    Awesome-o, thanks for that. I'll give it a go when I get back from work

  7. #7
    Join Date
    Mar 2007
    Location
    UK
    Beans
    141
    Distro
    Lubuntu

    Re: Py Script for looping over files

    Traceback (most recent call last):
    File "convert.py", line 8, in ?
    system(command) # Run command
    NameError: name 'system' is not defined


    I got this when I tried to run it. Should I have defined system before, or am I missing a module?

  8. #8
    Join Date
    Mar 2007
    Location
    /Earth/USA/MD/Home
    Beans
    826
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Py Script for looping over files

    your missing a module. just add to the top
    Code:
    from os import system
    In Soviet Russia, cake lies you!
    My Blog | Email Me

  9. #9
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: Py Script for looping over files

    Quote Originally Posted by [h2o] View Post
    Code:
    for file in avifiles:
    if you use any decent editor with syntax highlighting, you would notice that file has special color - it is reserved keyword and cannot be used as name. try infile instead.

  10. #10
    Join Date
    May 2007
    Beans
    244
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Py Script for looping over files

    Quote Originally Posted by pmasiar View Post
    if you use any decent editor with syntax highlighting, you would notice that file has special color - it is reserved keyword and cannot be used as name. try infile instead.
    Well that's interesting... gedit does color it differently, but on the other hand, it works. If file is a reserved word, why doesn't it cause an error? If you open python in interactive mode, it seems like you can use "file" however you want. That's kind of weird, isn't it?

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •