使用python递归查找文件
我找到了一个 如何递归移动所有文件,但我希望在目标文件夹中保持相同的文件夹结构使用python递归查找文件,python,recursion,shutil,Python,Recursion,Shutil,我找到了一个 如何递归移动所有文件,但我希望在目标文件夹中保持相同的文件夹结构 import fnmatch import os import shutil rootPath = '/Volumes/VoigtKampff/Temp/TEST/' destDir = '/Volumes/VoigtKampff/Temp/TEST2/' matches = [] for root, dirnames, filenames in os.walk(rootPath): for filenam
import fnmatch
import os
import shutil
rootPath = '/Volumes/VoigtKampff/Temp/TEST/'
destDir = '/Volumes/VoigtKampff/Temp/TEST2/'
matches = []
for root, dirnames, filenames in os.walk(rootPath):
for filename in fnmatch.filter(filenames, '*.mp4'):
matches.append(os.path.join(root, filename))
print(os.path.join(root, filename))
shutil.move(os.path.join(root, filename), os.path.join(destDir, filename))
如何修改上述代码以在目标文件夹中保持相同的文件夹结构?(此答案假设您使用的是Python 2.x)
您需要在运行时创建子目录:
import fnmatch
import os
import shutil
rootPath = '/Volumes/VoigtKampff/Temp/TEST/'
destDir = '/Volumes/VoigtKampff/Temp/TEST2/'
matches = []
for root, dirnames, filenames in os.walk(rootPath):
for filename in fnmatch.filter(filenames, '*.mp4'):
matches.append(os.path.join(root, filename))
print(os.path.join(root, filename))
targetDir = os.path.join(destDir, root[len(rootPath):])
if not os.path.exists(targetDir):
os.makedirs(targetDir)
shutil.move(os.path.join(root, filename), os.path.join(targetDir, filename))
还要注意,camelCase名称不是Python中的标准做法。看看PEP8:(这个答案假设您使用的是Python 2.x)
您需要在运行时创建子目录:
import fnmatch
import os
import shutil
rootPath = '/Volumes/VoigtKampff/Temp/TEST/'
destDir = '/Volumes/VoigtKampff/Temp/TEST2/'
matches = []
for root, dirnames, filenames in os.walk(rootPath):
for filename in fnmatch.filter(filenames, '*.mp4'):
matches.append(os.path.join(root, filename))
print(os.path.join(root, filename))
targetDir = os.path.join(destDir, root[len(rootPath):])
if not os.path.exists(targetDir):
os.makedirs(targetDir)
shutil.move(os.path.join(root, filename), os.path.join(targetDir, filename))
还要注意,camelCase名称不是Python中的标准做法。看看PEP8:(这个答案假设您使用的是Python 2.x)
您需要在运行时创建子目录:
import fnmatch
import os
import shutil
rootPath = '/Volumes/VoigtKampff/Temp/TEST/'
destDir = '/Volumes/VoigtKampff/Temp/TEST2/'
matches = []
for root, dirnames, filenames in os.walk(rootPath):
for filename in fnmatch.filter(filenames, '*.mp4'):
matches.append(os.path.join(root, filename))
print(os.path.join(root, filename))
targetDir = os.path.join(destDir, root[len(rootPath):])
if not os.path.exists(targetDir):
os.makedirs(targetDir)
shutil.move(os.path.join(root, filename), os.path.join(targetDir, filename))
还要注意,camelCase名称不是Python中的标准做法。看看PEP8:(这个答案假设您使用的是Python 2.x)
您需要在运行时创建子目录:
import fnmatch
import os
import shutil
rootPath = '/Volumes/VoigtKampff/Temp/TEST/'
destDir = '/Volumes/VoigtKampff/Temp/TEST2/'
matches = []
for root, dirnames, filenames in os.walk(rootPath):
for filename in fnmatch.filter(filenames, '*.mp4'):
matches.append(os.path.join(root, filename))
print(os.path.join(root, filename))
targetDir = os.path.join(destDir, root[len(rootPath):])
if not os.path.exists(targetDir):
os.makedirs(targetDir)
shutil.move(os.path.join(root, filename), os.path.join(targetDir, filename))
还要注意,camelCase名称不是Python中的标准做法。看看PEP8:为什么不直接做操作系统(“你的mv命令”)?为什么不直接做操作系统(“你的mv命令”)?为什么不直接做操作系统(“你的mv命令”)?为什么不直接做操作系统(“你的mv命令”)?将所有
.mp4
文件从root\u dir
目录递归移动到dest\u dir
目录,同时保留相对于root\u dir
的目录结构:
#!/usr/bin/env python3
from pathlib import Path
from shutil import move
src = Path(root_dir)
dest = Path(dest_dir)
for file in src.rglob('*.mp4'):
dest_file = dest / file.relative_to(src)
try:
dest_file.parent.mkdir(parents=True)
except OSError:
pass # ignore
move(str(file), str(dest_file))
它使用自Python 3.4以来在stdlib中可用的库。要在早期版本上安装它,请执行以下操作:
$ pip install pathlib
要将所有
.mp4
文件从root\u dir
目录递归移动到dest\u dir
目录,同时保留相对于root\u dir
的目录结构,请执行以下操作:
#!/usr/bin/env python3
from pathlib import Path
from shutil import move
src = Path(root_dir)
dest = Path(dest_dir)
for file in src.rglob('*.mp4'):
dest_file = dest / file.relative_to(src)
try:
dest_file.parent.mkdir(parents=True)
except OSError:
pass # ignore
move(str(file), str(dest_file))
它使用自Python 3.4以来在stdlib中可用的库。要在早期版本上安装它,请执行以下操作:
$ pip install pathlib
要将所有
.mp4
文件从root\u dir
目录递归移动到dest\u dir
目录,同时保留相对于root\u dir
的目录结构,请执行以下操作:
#!/usr/bin/env python3
from pathlib import Path
from shutil import move
src = Path(root_dir)
dest = Path(dest_dir)
for file in src.rglob('*.mp4'):
dest_file = dest / file.relative_to(src)
try:
dest_file.parent.mkdir(parents=True)
except OSError:
pass # ignore
move(str(file), str(dest_file))
它使用自Python 3.4以来在stdlib中可用的库。要在早期版本上安装它,请执行以下操作:
$ pip install pathlib
要将所有
.mp4
文件从root\u dir
目录递归移动到dest\u dir
目录,同时保留相对于root\u dir
的目录结构,请执行以下操作:
#!/usr/bin/env python3
from pathlib import Path
from shutil import move
src = Path(root_dir)
dest = Path(dest_dir)
for file in src.rglob('*.mp4'):
dest_file = dest / file.relative_to(src)
try:
dest_file.parent.mkdir(parents=True)
except OSError:
pass # ignore
move(str(file), str(dest_file))
它使用自Python 3.4以来在stdlib中可用的库。要在早期版本上安装它,请执行以下操作:
$ pip install pathlib
使用mv是不可能的吗?使用mv是不可能的吗?使用mv是不可能的吗?使用mv是不可能的吗?在python3,
os中。makedirs
包括一个绕过当前竞争条件的标志exist\u ok
here@AdamSmith——说得好;我在这里假设了一个Python2.x场景。刚刚注意到print(),所以我猜它应该是Python3!在python3中,os.makedirs
包含一个绕过当前竞争条件的标志exist\u ok
here@AdamSmith——说得好;我在这里假设了一个Python2.x场景。刚刚注意到print(),所以我猜它应该是Python3!在python3中,os.makedirs
包含一个绕过当前竞争条件的标志exist\u ok
here@AdamSmith——说得好;我在这里假设了一个Python2.x场景。刚刚注意到print(),所以我猜它应该是Python3!在python3中,os.makedirs
包含一个绕过当前竞争条件的标志exist\u ok
here@AdamSmith——说得好;我在这里假设了一个Python2.x场景。刚刚注意到print(),所以我猜它应该是Python3!可能独立于操作系统?可能独立于操作系统?可能独立于操作系统?可能独立于操作系统?