joz*_*yqk 3 python windows batch-file
所以我只是写了一个快速的python脚本来移动一些大型目录(所有在同一个驱动器上),错误地假设Windows命令行工具不是一个完整的笑话,并且move Root\Dir1 Root\Dir2会像Windows资源管理器GUI一样合并内容.我真的不在乎它是否替换或跳过文件夹中的重复文件,因为没有.
不幸的是(在管理员命令提示符下),
C:\>mkdir a
C:\>mkdir b
C:\>mkdir b\a
C:\>move b\a .
Overwrite C:\a? (Yes/No/All): yes
Access is denied.
... :O
... ?? really ??!?
... no, actually really really ???
Run Code Online (Sandbox Code Playgroud)
似乎唯一的方法是复制和删除.痛苦可怜.
有关:
我不是在编写代码来逐个复制文件.有没有办法实现文件夹移动与替换而不复制?
如果可能的话,我更愿意使用一些本机可执行文件.如果支持它,我也很乐意使用python.
python中的move-all-files-manual 解决方法.我还在愚蠢地挣扎.
def moveTree(sourceRoot, destRoot):
if not os.path.exists(destRoot):
return False
ok = True
for path, dirs, files in os.walk(sourceRoot):
relPath = os.path.relpath(path, sourceRoot)
destPath = os.path.join(destRoot, relPath)
if not os.path.exists(destPath):
os.makedirs(destPath)
for file in files:
destFile = os.path.join(destPath, file)
if os.path.isfile(destFile):
print "Skipping existing file: " + os.path.join(relPath, file)
ok = False
continue
srcFile = os.path.join(path, file)
#print "rename", srcFile, destFile
os.rename(srcFile, destFile)
for path, dirs, files in os.walk(sourceRoot, False):
if len(files) == 0 and len(dirs) == 0:
os.rmdir(path)
return ok
Run Code Online (Sandbox Code Playgroud)
如果有的话,请发一个正确的答案!
| 归档时间: |
|
| 查看次数: |
6271 次 |
| 最近记录: |