Python将文件从符合给定条件的目录移动到新目录

时间:2017-04-12 23:56:39

标签: 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
├── 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'

总结一下,如何在不移动文件夹本身的情况下将hw1,hw2等中包含的所有文件移动到./homework?如果这是xy problem并且实际上有更简单的方法,请指出我的方向。同样是的,我意识到在我花费大量时间进行调试和编写时,我可以很容易地手工完成,但这不是重点。

感谢。

2 个答案:

答案 0 :(得分:2)

试试这个:

<div><h2>RequestAnimationFrame (rAF)</h2>
rAF V rAF & setTimeout V setTimeout<br>
<canvas id = can1 width = 150></canvas>
<canvas id = can2 width = 150></canvas>
<canvas id = can3 width = 150></canvas><br>
Click the frame to set the current test.<br>
The left frame is using rAF alone, the middle using setTimeout and rAf, and the rigth frame uses setTimeout alone.<br>
Click <input type="button" id=load value="add Load"></input> to simulate a rendering load of around <input type="button" id=loadPlus value="14ms" title="click to change CPU load between 14 and 28ms"></input> <br>
   Try draging and selecting this text and see how it effects the different methods.<br>
rAF is by far the most stable of the 3.<br>
</div>

答案 1 :(得分:1)

你几乎就在那里。当你到达shutil.move(eachFile,source),&#39; eachFile&#39;这里只是您想要的文件的名称。例如,&#39; hw13.pdf&#39;。所以它会尝试在根路径中搜索它,但是没有&#39; hw13.pdf&#39;在根目录中(如异常消息所指出的那样)。

您需要做的就是将您要访问的文件夹的名称加入到您要移动的文件的名称中:

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)