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)

参考网址

原文地址:https://www.cnblogs.com/luyj00436/p/15433826.html