将文件移动到多个目的地
Moving files to multiple destination
我正在尝试将路径文件夹中的文件移动到多个目标目录。所以我的条件是 70% 的文件移动到 dest1,30% 的文件移动到 dest2。到目前为止我所尝试的给了我一些错误。我不确定逻辑是否错误或如何执行此操作。请 post 您的解决方案或想法。
谢谢
代码:
import os
import shutil
import random
from shutil import copyfile
path="/Users/kj/Downloads/spam_classifier-master2/data2/data"
dest1="/Users/kj/Downloads/test"
dest2="/Users/kj/Downloads/train"
files=os.listdir(path)
for f in files:
if (len(f) >0.7 ):
shutil.move(f,dest2)
elif (len(f)<0.3):
shutil.move(f,dest1)
错误:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 544, in move
os.rename(src, real_dst)
FileNotFoundError: [Errno 2] No such file or directory: 'mail0.txt' -> '/Users/kj/Downloads/train'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/kj/Downloads/ef.py", line 13, in <module>
shutil.move(f,dest2)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 558, in move
copy_function(src, real_dst)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 257, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'mail0.txt'
os.listdir
returns只是给定路径下的文件名,所以当你对文件名进行操作时,你应该先将文件名与路径连接起来以获得完整路径。您还应该将文件编号除以文件列表的长度以获得适当的比例:
files=os.listdir(path)
for i, f in enumerate(files):
if (i + 1) / len(files) > 0.7:
shutil.move(os.path.join(path, f),dest2)
else:
shutil.move(os.path.join(path, f),dest1)
我正在尝试将路径文件夹中的文件移动到多个目标目录。所以我的条件是 70% 的文件移动到 dest1,30% 的文件移动到 dest2。到目前为止我所尝试的给了我一些错误。我不确定逻辑是否错误或如何执行此操作。请 post 您的解决方案或想法。 谢谢
代码:
import os
import shutil
import random
from shutil import copyfile
path="/Users/kj/Downloads/spam_classifier-master2/data2/data"
dest1="/Users/kj/Downloads/test"
dest2="/Users/kj/Downloads/train"
files=os.listdir(path)
for f in files:
if (len(f) >0.7 ):
shutil.move(f,dest2)
elif (len(f)<0.3):
shutil.move(f,dest1)
错误:
Traceback (most recent call last):
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 544, in move
os.rename(src, real_dst)
FileNotFoundError: [Errno 2] No such file or directory: 'mail0.txt' -> '/Users/kj/Downloads/train'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/kj/Downloads/ef.py", line 13, in <module>
shutil.move(f,dest2)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 558, in move
copy_function(src, real_dst)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 257, in copy2
copyfile(src, dst, follow_symlinks=follow_symlinks)
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 120, in copyfile
with open(src, 'rb') as fsrc:
FileNotFoundError: [Errno 2] No such file or directory: 'mail0.txt'
os.listdir
returns只是给定路径下的文件名,所以当你对文件名进行操作时,你应该先将文件名与路径连接起来以获得完整路径。您还应该将文件编号除以文件列表的长度以获得适当的比例:
files=os.listdir(path)
for i, f in enumerate(files):
if (i + 1) / len(files) > 0.7:
shutil.move(os.path.join(path, f),dest2)
else:
shutil.move(os.path.join(path, f),dest1)