У меня есть сценарий python, который сначала обнаруживает съемные устройства, а затем копирует файлы с этого съемного устройства в новое место назначения.
Я создал функцию для обнаружения съемных устройств и вторую функцию для копирования с файлы.
но проблема, которую я не знаю, как сделать обнаруженное устройство в качестве источника для функции копирования.
Пока я жестко закодировал источник, но это не очень хорошая практика.
код:
import win32api
import win32file
import calendar
import os
import shutil
from os import path
from datetime import date
def main():
detectUSB()
copy()
def detectUSB():
# Returns a list containing letters from removable drives
drive_list = win32api.GetLogicalDriveStrings()
drive_list = drive_list.split("\x00")[0:-1] # the last element is ""
list_removable_drives = []
for letter in drive_list:
if win32file.GetDriveType(letter) == win32file.DRIVE_REMOVABLE:# check if the drive is of type removable
list_removable_drives.append(letter)
print("list drives: {0}".format(letter))
return list_removable_drives
src = "I:\\" ## this is where i hardcoded the source
def copy():
try:
for dirpath, dirnames, files in os.walk(src):
print(f'Found directory: {dirpath}')
# for file_name in files:
if len(dirnames)==0 and len(files)==0:
print("this directory is empty")
else:
print(files)
except Exception as e:
print(e)
if os.path.exists(dst):
shutil.rmtree(dst)
print("the deleted folder is :{0}".format(dst))
#copy the folder as it is (folder with the files)
copieddst = shutil.copytree(src2,dst)
print("copy of the folder is done :{0}".format(copieddst))
else:
#copy the folder as it is (folder with the files)
copieddst = shutil.copytree(src2,dst)
print("copy of the folder is done :{0}".format(copieddst))
if __name__=="__main__":
main()
, поэтому я хочу сделать в качестве аргумента возвращаемую переменную detectUSB () sr c для функции copy () .