如何使用python将放置在多个嵌套文件夹中的文档移动和重命名到新的单个文件夹中?
我在几个文件夹中有几个文件,如下所示:如何使用python将放置在多个嵌套文件夹中的文档移动和重命名到新的单个文件夹中?,python,python-3.x,directory,glob,Python,Python 3.x,Directory,Glob,我在几个文件夹中有几个文件,如下所示: dir ├── 0 │ ├── 103425.xml │ ├── 105340.xml │ ├── 109454.xml │ │── 1247 │ └── doc.xml ├── 14568 │ └── doc.xml ├── 1659 │ └── doc.xml ├── 10450 │ └── doc.xml ├── 10351 │ └── doc.xml 如何将所有文档提取到单个文件夹中,并为每个移动的文档添加文件夹名
dir
├── 0
│ ├── 103425.xml
│ ├── 105340.xml
│ ├── 109454.xml
│
│── 1247
│ └── doc.xml
├── 14568
│ └── doc.xml
├── 1659
│ └── doc.xml
├── 10450
│ └── doc.xml
├── 10351
│ └── doc.xml
如何将所有文档提取到单个文件夹中,并为每个移动的文档添加文件夹名称:
new_dir
├── 0_103425.xml
├── 0_105340.xml
├── 0_109454.xml
├── 1247_doc.xml
├── 14568_doc.xml
├── 1659_doc.xml
├── 10450_doc.xml
├── 10351_doc.xml
我试图用以下方法提取它们:
import os
for path, subdirs, files in os.walk('../dir/'):
for name in files:
print(os.path.join(path, name))
更新
此外,我还试图:
import os, shutil
from glob import glob
files = []
start_dir = os.getcwd()
pattern = "*.xml"
for dir,_,_ in os.walk('../dir/'):
files.extend(glob(os.path.join(dir,pattern)))
for f in files:
print(f)
shutil.move(f, '../dir/')
上面给出了每个文件的路径。但是,我不明白如何重命名和移动它们:
---------------------------------------------------------------------------
Error Traceback (most recent call last)
<ipython-input-50-229e4256f1f3> in <module>()
10 for f in files:
11 print(f)
---> 12 shutil.move(f, '../dir/')
/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py in move(src, dst, copy_function)
540 real_dst = os.path.join(dst, _basename(src))
541 if os.path.exists(real_dst):
--> 542 raise Error("Destination path '%s' already exists" % real_dst)
543 try:
544 os.rename(src, real_dst)
Error: Destination path '../data/230948.xml' already exists
---------------------------------------------------------------------------
错误回溯(最近一次呼叫上次)
在()
10对于文件中的f:
11印刷品(f)
--->12 shutil.move(f'../dir/')
/usr/local/ceral/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py in-move(src、dst、copy_函数)
540 real_dst=os.path.join(dst,_basename(src))
541如果os.path.存在(实际测试):
-->542引发错误(“目标路径“%s”已存在”%realdst)
543尝试:
544 os.重命名(src,realDST)
错误:目标路径“../data/230948.xml”已存在
上面的错误显示了我为什么要将其重命名为文件夹。这对您有什么作用
import os
import pathlib
OLD_DIR = 'files'
NEW_DIR = 'new_dir'
p = pathlib.Path(OLD_DIR)
for f in p.glob('**/*.xml'):
new_name = '{}_{}'.format(f.parent.name, f.name)
f.rename(os.path.join(NEW_DIR, new_name))
如果您没有Python(3.5+)的现代版本,也可以使用glob、os和shutil:
import os
import glob
import shutil
for f in glob.glob('files/**/*.xml'):
new_name = '{}_{}'.format(os.path.basename(os.path.dirname(f)), os.path.basename(f))
shutil.move(f, os.path.join('new_dir', new_name))
使用Python3的新模块进行路径操作,然后将文件移动到正确的位置,这是最容易做到的。与之不同,
shutil.move
将像mv
命令一样工作,即使在跨文件系统移动时也能正常工作
此代码适用于嵌套到任何级别的路径-路径中的任何/
或\
都将替换为目标文件名中的
,因此dir/foo/bar/baz/xyzy.xml
将移动到新的\u dir/foo\u bar\u baz\xyzy.xml
from pathlib import Path
from shutil import move
src = Path('dir')
dst = Path('new_dir')
# create the target directory if it doesn't exist
if not dst.is_dir():
dst.mkdir()
# go through each file
for i in src.glob('**/*'):
# skip directories and alike
if not i.is_file():
continue
# calculate path relative to `src`,
# this will make dir/foo/bar into foo/bar
p = i.relative_to(src)
# replace path separators with underscore, so foo/bar becomes foo_bar
target_file_name = str(p).replace('/', '_').replace('\\', '_')
# then do rename/move. shutil.move will always do the right thing
# note that it *doesn't* accept Path objects in Python 3.5, so we
# use str(...) here. `dst` is a path object, and `target_file_name
# is the name of the file to be placed there; we can use the / operator
# instead of os.path.join.
move(str(i), str(dst / target_file_name))
我还添加了如何使用shutil、os和glob实现这一点。实际上,我使用的是python3!谢谢安蒂,伟大的安西!