Python将符合给定条件的目录中的文件移动到新目录
我有一个类似以下内容的目录:Python将符合给定条件的目录中的文件移动到新目录,python,file,mv,Python,File,Mv,我有一个类似以下内容的目录: . ├── files.py ├── homework ├── hw1 │ └── hw1.pdf ├── hw10 │ └── hw10.pdf ├── hw13 │ └── hw13.pdf ├── hw2 │ └── hw2.pdf ├── hw3 │ └── hw3.pdf ├── hw4 │ └── hw4.pdf ├── hw7 │ └── hw7.pdf ├── IntroductionToAlgorithms.pdf ├─
.
├── files.py
├── homework
├── hw1
│ └── hw1.pdf
├── hw10
│ └── hw10.pdf
├── hw13
│ └── hw13.pdf
├── hw2
│ └── hw2.pdf
├── hw3
│ └── hw3.pdf
├── hw4
│ └── hw4.pdf
├── hw7
│ └── hw7.pdf
├── IntroductionToAlgorithms.pdf
├── p157
│ └── Makefile
├── p164
│ └── project
├── p171
│ ├── project
├── p18
│ └── project
├── p246
│ ├── project
├── p257
│ ├── project
├── p307
│ ├── project
├── p34
│ └── project
├── p363
│ ├── project
├── p431
│ ├── bit_buffer.h
├── p565
│ ├── project
├── p72
│ └── project
├── README.md
└── tree.txt
我想将hwN文件夹中的所有文件移动到家庭作业中。
示例作业将包含
hw1.pdf->hw13.pdf,不保留任何名为hwN的文件夹
其中N是编号的作业文件夹之一
我有一个python脚本,它几乎可以工作:
files.py:
import os
import shutil
if not os.path.exists("homework"):
os.makedirs("homework")
print("created hw directory")
source='/home/kalenpw/Documents/School/2017Spring/CS3385/homework/'
files = os.listdir()
for f in files:
if f.startswith("hw") and len(f) > 2:
#This line works but it keeps the subfolders where I want the files directly in ./homework
shutil.move(f, source)
# for eachFile in os.listdir(f):
# #Ideally this would move all the files within the hw folders and move just the file not the folder to my source
# shutil.move(eachFile, source)
但是,我试图使用注释掉的代码仅移动文件而不是文件夹,导致此错误:
Traceback (most recent call last):
File "/usr/lib/python3.5/shutil.py", line 538, in move
os.rename(src, real_dst)
FileNotFoundError: [Errno 2] No such file or directory: 'hw13.pdf' -> '/home/kalenpw/Documents/School/2017Spring/CS3385/homework/hw13.pdf'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "files.py", line 17, in <module>
shutil.move(eachFile, source)
File "/usr/lib/python3.5/shutil.py", line 552, in move
copy_function(src, real_dst)
File "/usr/lib/python3.5/shutil.py", line 251, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/usr/lib/python3.5/shutil.py", line 114, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'hw13.pdf'
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python3.5/shutil.py”,第538行,移动中
重命名(src,realdst)
FileNotFoundError:[Errno 2]没有这样的文件或目录:“hw13.pdf'->'/home/kalenpw/Documents/School/2017Spring/CS3385/homogy/hw13.pdf”
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“files.py”,第17行,在
shutil.move(每个文件,源)
文件“/usr/lib/python3.5/shutil.py”,第552行,移动中
复制_函数(src、real_-dst)
文件“/usr/lib/python3.5/shutil.py”,第251行,copy2
复制文件(src、dst、follow_symlinks=follow_symlinks)
copyfile中的文件“/usr/lib/python3.5/shutil.py”,第114行
开放式(src,'rb')作为fsrc:
FileNotFoundError:[Errno 2]没有这样的文件或目录:“hw13.pdf”
总而言之,在不移动文件夹的情况下,如何将hw1、hw2等中包含的所有文件移动到./homography?如果是这样的话,事实上有一个更简单的方法,请告诉我这个方向。是的,我意识到,在我花了大量时间调试和编写这篇文章的过程中,我可以很容易地手工完成,但这不是重点
谢谢。试试这个:
from os import walk, path
source='/home/kalenpw/Documents/School/2017Spring/CS3385/homework/'
for (dirpath, dirnames, filenames) in walk(source):
for file in filenames:
shutil.move(path.join(dirpath,file), source)
试试这个:
from os import walk, path
source='/home/kalenpw/Documents/School/2017Spring/CS3385/homework/'
for (dirpath, dirnames, filenames) in walk(source):
for file in filenames:
shutil.move(path.join(dirpath,file), source)
你快到了。当您访问shutil.move(eachFile,source)时,“eachFile”在这里只是您想要的文件名。例如,“hw13.pdf”。因此,它将尝试在根路径中搜索它,但根路径中没有“hw13.pdf”(正如异常消息所指出的) 您需要做的只是将您所在文件夹的名称与要移动的文件的名称连接起来:
for f in files:
if f.startswith("hw") and len(f) > 2:
for eachFile in os.listdir(f):
filePath = os.path.join(f, eachFile)
shutil.move(filePath, source)
你快到了。当您访问shutil.move(eachFile,source)时,“eachFile”在这里只是您想要的文件名。例如,“hw13.pdf”。因此,它将尝试在根路径中搜索它,但根路径中没有“hw13.pdf”(正如异常消息所指出的) 您需要做的只是将您所在文件夹的名称与要移动的文件的名称连接起来:
for f in files:
if f.startswith("hw") and len(f) > 2:
for eachFile in os.listdir(f):
filePath = os.path.join(f, eachFile)
shutil.move(filePath, source)