Python Forum
My First Python Script. Feedback Sought.
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My First Python Script. Feedback Sought.
#1
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.
Reply


Messages In This Thread
My First Python Script. Feedback Sought. - by malonn - Jun-21-2018, 01:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb Script broke with Python 3 / improve script for printing aspecteleven 4 4,682 Feb-22-2019, 12:23 PM
Last Post: buran
  Feedback on my first Python module CodeRaker 11 6,790 Jun-21-2018, 02:16 AM
Last Post: ichabod801
  Requesting feedback on this learning script I_love_py 2 2,953 Dec-01-2017, 12:22 PM
Last Post: I_love_py

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020