Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

My program is written in python and I use pythontidy and reindent to clean it. However I'd like a more efficient method than renaming the file, should I just look at the doc to know how to now get forced to rename files and make pythontidy behave more like reindent i.e. enable it to run on all files in a directory just by running it:

$ python ./PythonTidy-1.20.py mai 
mailman.py   main.old.py  main.py      main.py.bak  main.pyc     
ubuntu@ubuntu:/media/Lexar/montao/montaoproject$ python ./PythonTidy-1.20.py main.py main.tidy.py
ubuntu@ubuntu:/media/Lexar/montao/montaoproject$ python ./PythonTidy-1.20.py i18n.py i18n.tidy.py
ubuntu@ubuntu:/media/Lexar/montao/montaoproject$ mv i18n.tidy.py i18n.py
ubuntu@ubuntu:/media/Lexar/montao/montaoproject$ mv main.tidy.py main.py
ubuntu@ubuntu:/media/Lexar/montao/montaoproject$ python ./reindent .
python: can't open file './reindent': [Errno 2] No such file or directory
ubuntu@ubuntu:/media/Lexar/montao/montaoproject$ python ./reindent.py .
reindented ./appengine_config.py 
reindented ./i18n.py 
reindented ./main.py 
reindented ./util.py 
reindented ./facebookapi.py 
reindented ./br.py 
reindented ./PythonTidy-1.20.py 
reindented ./in.old.py 
reindented ./login_required.py 
ubuntu@ubuntu:/media/Lexar/montao/montaoproject$ 
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I'd personally use a Python (or shell, perl, etc.) script to do this. You could use a find command and do some things to each .py file found, but for me this is approaching the line (3 commands; reindent, pythontidy, mv,) where I would write a script instead of trying to get such a complex command right. I'm guessing that'd be this, but haven't tried it:

find . -name '*.py' -exec python reindent.py {} \; \
 -exec python PythonTidy-1.20.py {} > temp \; -exec mv temp {} \;

The docs for PythonTidy say - well, I can't find any.

The code for PythonTidy makes it look like this tidying in-place is not an option. Apparently the script lets you output to stdout (and read input from stdin). So my first thought is was that we could execute it like this:

# Don't use this!!!
% python PythonTidy.py testfile.py > testfile.py

Whether this is a good idea or not depends on the implementation of PythonTidy; if it reads the whole file in before opening the output we're fine. But it doesn't doesn't, so bad idea.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.