Python之復制文件目錄和快捷方式


需求

假設現在需要將某一個目錄A下的文件生成快捷方式到另一個目錄B下。具體要求如下(可以參考下圖):

  • 在目錄A的第一級子目錄下的內容如果為文件夾,則目錄B子目錄創建同名文件夾;如果是文件,則創建該文件的快捷方式。
  • 在目錄A的子目錄的子目錄下,創建文件或文件夾的快捷方式。

實現思路

1. 循環可遞歸判斷當前文件夾下的子目錄。

這里需要引用os—— import os ;獲取當前目錄的文件/文件夾列表方法為 os.listdir(path) # path表示目錄名稱 ;判斷是否為文件的方法為:  os.path.isfile(file) #file表示文件的完整路徑  ;如果不存在則創建文件夾的方法為:  os.makedirs(path)#path表示文件夾路徑

2. 創建快捷方法。

創建快捷方式的代碼如下:

 1 import pythoncom
 2 import  os
 3 from win32com.shell import shell, shellcon
 4 '''
 5 sourceName 文件名稱完整路徑
 6 targetName 快捷鍵名稱完整路徑
 7 icon 圖標完整路徑——如無需特別設置,可去掉iconname 參數
 8 '''
 9 def setShortcut(sourceName ="" , targetName="" , iconName = ""):
10     try:
11         shortcut = pythoncom.CoCreateInstance(
12             shell.CLSID_ShellLink, None,
13             pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
14         # 獲取接口
15         persist = shortcut.QueryInterface(pythoncom.IID_IPersistFile)
16         # 設置數據
17         shortcut.SetPath(sourceName)
18         shortcut.SetIconLocation(iconName, 0)  # 可有可無,沒有就默認使用文件本身的圖標
19         # 保存快捷文件位置
20         persist.Save(targetName, 0)
21         return  True
22     except Exception as e:
23         print(e.args)
24         return False

源碼

 1 import pythoncom
 2 import  os
 3 from win32com.shell import shell, shellcon
 4 
 5 sourcePath = 'D:\\臨時\\劇本殺大全\\1..經典本'
 6 targetPath = 'D:\\臨時\\劇本殺大全\\已閱讀'
 7 
 8 '''
 9 sourceName 文件名稱完整路徑
10 targetName 快捷鍵名稱完整路徑
11 icon 圖標完整路徑——如無需特別設置,可去掉iconname 參數
12 '''
13 def setShortcut(sourceName ="" , targetName="" , iconName = ""):
14     try:
15         shortcut = pythoncom.CoCreateInstance(
16             shell.CLSID_ShellLink, None,
17             pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
18         # 獲取接口
19         persist = shortcut.QueryInterface(pythoncom.IID_IPersistFile)
20         # 設置數據
21         shortcut.SetPath(sourceName)
22         shortcut.SetIconLocation(iconName, 0)  # 可有可無,沒有就默認使用文件本身的圖標
23         # 保存快捷文件位置
24         persist.Save(targetName, 0)
25         return  True
26     except Exception as e:
27         print(e.args)
28         return False
29 # 保存文件的快捷目錄到另一個文件夾(1.小於層級時,如果是目錄,則復制目錄;如果是文件,生成快捷方式)
30 # sourcePath 源目錄
31 # targetPath 目標目錄
32 # level
33 def saveShortCuts(sourcePath, targetPath , level = 2) :
34     try:
35         # 判斷源目錄和目標目錄是否存在(目標目錄不存在則創建)
36         if os.path.exists(sourcePath) == False :
37             return False
38         if os.path.exists(targetPath) == False:
39             os.makedirs(targetPath)
40 
41         # 循環遞歸獲取當前文件夾
42         tempPaths = os.listdir(sourcePath)
43         for tempPath in tempPaths:
44             if os.path.isfile(sourcePath +'\\' +tempPath) or level <= 1:
45                 # 類型為文件 或 等級小於等於1 ,創建快捷方式
46                 setShortcut(sourceName=sourcePath + '\\' + tempPath , targetName= targetPath + '\\' + tempPath + '.lnk')
47             else:
48                 # 文件類型為文件夾 且等級大於1 ,創建當前目錄,並遞歸調用
49                 os.makedirs(targetPath +  '\\' + tempPath )
50                 saveShortCuts(sourcePath = sourcePath + '\\' + tempPath , targetPath = targetPath +  '\\' + tempPath , level = level -1)
51 
52         return True
53     except Exception as e:
54         print(e.args)
55         return False
56 
57 result = saveShortCuts(sourcePath = sourcePath , targetPath = targetPath)
58 print("返回結果" , result)

參考網址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM