[英]Moving folders inside of folders
I have folders named from 00
to ff
(all lower-cased) that have a random number of folders inside them named randomly as well.我有从
00
到ff
(全部小写)命名的文件夹,它们内部也有随机命名的文件夹。 I need to only move the folders inside to a different location.我只需要将里面的文件夹移动到不同的位置。
folders = list((range(256)))
for i in range(256):
folders[i] = hex(folders[i])[2:4]
if len(folders[i]) == 1:
folders[i] = "0" + folders[i]
for i in range(len(folders)):
shutil.move(f"D:\folders\{folders[i]\*}", "D:\MainFolder")
I am expecting all the files inside D:\\folders\\(00)
to move into D:\\Mainfolder
and to repeat until all the files are moved in but it's throwing an error:我期待
D:\\folders\\(00)
所有文件移动到D:\\Mainfolder
并重复直到所有文件都移入但它抛出错误:
OSError: [Errno 22] Invalid argument: 'D:\\folders\\00\\*'
Also, was there any way to improve the way I made the array?另外,有什么方法可以改进我制作阵列的方式吗?
您需要将命令修改为: shutil.move(f"D:\\folders\\{folders[i]}\\*","D:\\MainFolder")
shutil.move
is expecting to get an explicit path as an argument. shutil.move
期望获得显式路径作为参数。 It seems that you are confusing with glob
that can take the path with shell-like wildcards.似乎您对
glob
感到困惑,它可以使用类似 shell 的通配符作为路径。 I am assuming that with the *
you mean to move anything under that folder, but that is not necessary.我假设使用
*
您的意思是移动该文件夹下的任何内容,但这不是必需的。 As the docs state:正如文档所述:
Recursively move a file or directory (src) to another location (dst)
递归地将文件或目录 (src) 移动到另一个位置 (dst)
(emphasis mine). (强调我的)。
As a side note, you can obtain the folders
list much easier, by using string formatting :作为旁注,您可以通过使用字符串格式更轻松地获取
folders
列表:
folders = [f"{hex(i)[2:]:0>2}" for i in range(256)]
Or simply avoid saving such a list in memory and just do:或者干脆避免将这样的列表保存在内存中,只需执行以下操作:
for i in range(256):
shutil.move(f"D:\folders\{hex(i)[2:]:0>2}", "D:\MainFolder")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.