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

user8900239 picture user8900239 · Nov 20, 2017 · Viewed 15.9k times · Source

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?

Answer

Steinfeld picture Steinfeld · Aug 6, 2018

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)