Need HELP with a Python script

Discussion in 'Mac Programming' started by BlueFielder, Sep 7, 2013.

  1. BlueFielder, Sep 7, 2013
    Last edited: Sep 7, 2013

    BlueFielder macrumors newbie

    Sep 7, 2013
    I'm NOT a programmer…. but I have a little Python script on OS X that converts files, that are in a folder, from one format to another.

    Script =

    I can successfully run it from the command line:

    1st, I placed the folder that I named 'ddd' on the desktop so that it's easy to get to.

    2. Terminal : cd ~/Desktop/ddd

    3. Then I run the script from terminal : python ./ ./ aumu Alb3 LinP vstdata

    It executes fine and does it's job.

    BUT… I have per 7600 files that are segregated into 86 folders.
    I want to keep this folder structure.

    So, What I need the script to do is to start at the parent folder (ddd) and then go through each subordinate (child) folder and perform its task on all files.

    I found this syntax on the web that is somehow supposed to do what I need:

    for /r %a in (*.fxp) do "%a"

    BUT … I have no idea how to combine the syntax.

    Could someone put this together for me please.
    Something that I could just copy and paste into Terminal.
    I would be most grateful.

    Thank you very much!
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    I'm confused by your description... where is the .py file located? Where are the files you want converted? What arguments does it accept? It seems to me that there's probably a one liner for bash that can recurse through your hierarchy and invoke the .py script on each of them, although my bash skills are very weak (I know cd and ls... That's about all I can do without looking it up), so I don't know what exactly it would look like.

    I could probably easily tweak your Python script to handle this if you want to share it... I have butt tons of Python experience.

    Thinking about it, maybe you could even just use the Python shell and call the script from within it, no need to touch bash or the existing Python script... I'll think about it and maybe post what you need to type into the Python shell, if I remember to come back...
  3. mrichmon macrumors 6502a

    Jun 17, 2003
    Assuming that one of the strings in the command is the input filename, you can do this with a simple shell command.

    Create a folder named "data" on the Desktop and copy all your data files and folders containing data files into the Desktop/data folder.

    Create a folder named "fps2au" on the Desktop and copy the python script "" into this folder. Setting up these two folders is optional but define the setup for the following commands.

    In the terminal, execute "cd ~/Desktop/data" to change into the data folder.

    The command "find . -type f" should produce a list of all of your input data files.

    The following command will transform all your files assuming that the filename in your example is "vstdata":

    find . -type f -print0 | xargs -0 -J %% python ~/Desktop/fxp2au/ ./ aumu Alb3 LinP %%
    What this does is:
    • "find . -type f -print0" prints a list of all the files under the current directory separated by NULL characters.
    • The command "xargs -0 -J %%" expects as input a list of filenames separated by NULL characters. For each filename, it takes the remainder of the command line, replaces "%%" with the filename and then invokes the resulting command. xargs keeps doing this until there are no more filenames piped into it.
  4. BlueFielder thread starter macrumors newbie

    Sep 7, 2013
    Thank you for your reply sir!

    Sorry … I should have mentioned that , for simplicity (and my lack of any knowledge of this stuff), I placed the .py file in the folder that Terminal has in focus …. In this case, the test folder (I placed on the Desktop, again for simplicity) that the files are in.
    And , Yes, I now realize that is not going to work as the script moves through any subordinate folder… and will not be able to see the .py file.
  5. BlueFielder thread starter macrumors newbie

    Sep 7, 2013

    Thank you VERY much for your time and reply.

    I just did all that you outlined and …. it failed.
    I then noticed that , I think', you left off part of the file syntax. "vstdata".

    So I 'edited' your script:
    find . -type f -print0 | xargs -0 -J %% python ~/Desktop/fxp2au/ ./ aumu Alb3 LinP vstdata %%

    But that too failed. Here is the result :
    usage: fxp2aupreset path type subtype manufacturer state_key
    Incorrect number of parameters specified.

    NOTE: Terminal returned the same error for both versions. Your version and my edited one.

    So sorry to be so dense on all this.
  6. pitaya macrumors member

    Jun 17, 2012
    that's not a bash for loop.

    if you want to write one in bash, since you know the directory setup:

    cd ddd/
    for d in *; do 
         python ./ "$d" aumu Alb3 LinP vstdata
    this is usage info from your python script which shows that the first argument is a path. that's one of the problems with the find/xargs command. it adds the full path to all files to the end of the python command, giving it too many arguments. it needs some changes, like -n, and it needs to either cd into/out of each directory and run the python script, or replace ./ with each directory

Share This Page