Uncompyle6 convert pyc to py file python 3 (Whole directory)

25.8k Views Asked by At

I have 200 pyc files I need to convert in a folder. I am aware of converting pyc to py files through uncompyle6 -o . 31.pyc however as I have so many pyc files, this would take a long period of time. I've founds lots of documentation but not much in bulk converting to py files. uncompyle6 -o . *.pyc was not supported.

Any idea on how I can achieve this?

4

There are 4 best solutions below

2
On BEST ANSWER

In operating systems with shell filename expansion, you might be able to use the shell's file expansion ability. For example:

uncompyle6 -o /tmp/unc6 myfiles/*.pyc

If you need something fancier or more control, you could always write some code that does the fancier expansion. Here is the above done in POSIX shell filtering out the single file myfiles/huge.pyc:

cd myfiles
for pyc in *.pyc; do 
   if [[ $pyc != huge.pyc ]] ; then 
     uncompyle -o /tmp/unc $pyc
   fi 
done

Note: It seems this question was also asked in Issue on output directory while executing commands with windows batch command "FOR /R"

0
On

thank you for the code, extending it to recursively call, nested sub directories, save as uncompile.py, in the directory to be converted, to run in command prompt type "python uncomple.py" would convert pyc to py in current working directory, with error handling and if rerun skips (recovery) files checking existing py extension match

import os
import uncompyle6

#Use current working directory
your_directory = os.getcwd()

#function processing current dir
def uncompilepath(mydir):
    for dirpath, b, filenames in os.walk(mydir):
        for d in b:
            folderpath = dirpath + '/' + d
            print(folderpath)

            #recursive sub dir call
            uncompilepath(folderpath)
        for filename in filenames:
            if not filename.endswith('.pyc'):
                continue
            filepath = dirpath + '/' + filename
            original_filename = filename.split('.')[0]
            original_filepath = dirpath + '/' + original_filename + '.py'

            #ignore if already uncompiled
            if os.path.exists(original_filepath):
                continue
            with open(original_filepath, 'w') as f:
                print(filepath)
                
               #error handling
                try:
                    uncompyle6.decompile_file(filepath, f)
                except Exception:
                    print("Error")
                
uncompilepath(your_directory)
0
On

Might not be perfect but it worked great for me.

import os
import uncompyle6
your_directory = ''
for dirpath, b, filenames in os.walk(your_directory):
    for filename in filenames:
        if not filename.endswith('.pyc'):
            continue

        filepath = dirpath + '/' + filename
        original_filename = filename.split('.')[0]
        original_filepath = dirpath + '/' + original_filename + '.py'
        with open(original_filepath, 'w') as f:
            uncompyle6.decompile_file(filepath, f)
0
On

This is natively supported by uncompyle6

uncompyle6 -ro <output_directory> <python_directory>
  • -r tells the tool to recurse into sub directories.
  • -o tells the tool to output to the given directory.