Python Forum

Full Version: My First Python Script. Feedback Sought.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Other than practice exercises found on the internet, this is my first foray into (what will be a long relationship with) Python. I would love to hear what the pros and veterans think about this sloppy mess. I'm at a point where I don't see how I could refine it any (noob/novice here). It was quite fun to write.

import ctypes, string, os, ffmpy, subprocess, sys, shutil

def get_drive_letters():
    drive_mask = ctypes.WinDLL('Kernel32').GetLogicalDrives()
    drives = []
    for Z in string.ascii_uppercase:
        if drive_mask & 1:
            drives.append(Z + ':\\')
        drive_mask >>= 1
    return drives

def get_drive_name(drive_letters):
    drive_name = ctypes.create_unicode_buffer(1024)
    file_system = ctypes.create_unicode_buffer(1024)
    drive_names = {}
    for drive in drive_letters:
        ctypes.WinDLL('Kernel32').GetVolumeInformationW(
            ctypes.c_wchar_p(drive),
            drive_name,
            ctypes.sizeof(drive_name),
            None,
            None,
            None,
            file_system,
            ctypes.sizeof(file_system)
            )
        drive_names.setdefault(drive_name.value, drive)
    return drive_names

def find_FFProbe(base_dir):
    for root, dirs, files in os.walk(base_dir):
        for File in files:
            if File == 'probe.exe':
                return os.path.join(root, File)

def get_dimensions(input_video, probe_path):
    args = '-v error -select_streams v:0 -show_entries stream=width,height ' \
           '-of compact'
    call = ffmpy.FFprobe(probe_path, args, {input_video : None})
    return call.run(None, subprocess.PIPE)

def get_height(tuple_in):
    a = str(tuple_in[0])
    b = a.rfind('=')
    return a[b + 1:b + 4]

def transfer_files(move, copy, move_to, copy_to):
    shutil.move(move, move_to)
    full_path = copy_to
    with open(copy, 'rb') as f1, open(copy_to, 'wb') as f2:
        full_size = os.stat(copy).st_size
        full = 0
        increment = 10485760
        while full < full_size:
            chunk = f1.read(increment)
            full += increment
            if full > full_size:
                full = full_size
            f2.write(chunk)
            print(round(full / full_size * 100, 1), '%\r')

to_move = sys.argv[1] #file dragged onto script
#check drives, eliminating OS and non-fixed, and return a dictionary with-
#drive names and letters===:
temp_list = []
for X in get_drive_letters():
    if ctypes.WinDLL('Kernel32').GetDriveTypeW(ctypes.c_wchar_p(X)) == 3 \
    and X != 'C:\\':
        temp_list.append(X)
drives_dict = get_drive_name(temp_list)
#===

output = get_dimensions(to_move, find_FFProbe(drives_dict['Back-Ups'] + \
         'App Back-Ups'))
height = get_height(output)
if height == '480':
    path_str_1 = drives_dict['Back-Ups'] + 'My Movies\DVD Rips\\'
    path_str_2 = drives_dict['Movies'] + 'My Movies\DVD Rips'
else:
    path_str_1 = drives_dict['Back-Ups'] + 'My Movies\Blu-ray Rips\\'
    path_str_2 = drives_dict['Movies'] + 'My Movies\Blu-ray Rips'

last_sep = to_move.rfind('\\')
to_copy = path_str_1 + to_move[last_sep + 1:]
path_str_3 = path_str_2 + '\\' + to_move[last_sep + 1:]
transfer_files(to_move, to_copy, path_str_1, path_str_3)
I look forward to replies.
I didn't read the code all the way through, however, I did notice one thing. You lack documentation. One thing you should practice is documentation(ex. a comment above one of your functions explaining what it does.) This gives other people, like us, an idea on what your code does, with minimal reading of the code. It also helps when you go back on a future date and work on the code again. Generally, it's just good practice. Hope this helps a bit.
I would agree with Zombie_Programming, but I would suggest using a triple quoted string for the line after the def statement:

def func(a):
    """Adds four."""
    return a + 4
Then when someone types help(func) in the interactive console, it will give them that text.
Oh, okay. Comments/documentation. I can certainly do that. I'll go ahead and do that and post back the newly documented code. I thought I wasn't getting any replies because my code is solid, but now I'm thinking it's because people don't feel like reading it and figuring out what I'm trying to do.
Good tips, thanks you two.
(Jun-29-2018, 01:49 PM)malonn Wrote: [ -> ]I thought I wasn't getting any replies because my code is solid

I didn't comment because I didn't want to test it. I have an aversion to letting strange programs mess with my file system.
For it being one of your first Python scripts, it does look solid. I just have a knack for documentation and making code look "Pretty." Hope this helps in the long run :)