我试着把小麦中的220个文件移到train-unreuters文件包中,小麦中的另一个文件移到train-unreuters-test-unreuters文件包中,但是当我运行代码时,它会给我一个错误,我实际上把文件放在了正确的位置!我怎样才能解决这个问题?
#!/usr/bin/python
#coding:utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import os
import os.path
import shutil
import random
path = '/home/user1/zhouchun/lda/reuters-21578/Wheat'
targetpath1 = '/home/user1/zhouchun/lda/reuters-21578/train_reuters'
targetpath2 = '/home/user1/zhouchun/lda/reuters-21578/test_reuters'
list=random.sample(range(1, 306),220)
for i in list:
file_dir = os.path.join(path, str(i))
# print file_dir
shutil.move(file_dir, targetpath1)
files = os.listdir(path)
for file in files:
# print file
dir = os.path.join(path, file)
if dir != file_dir:
shutil.move(dir, targetpath2)
请检查
ome/user1/zhouchun/lda/reuters-21578/Wheat
文件编号是否为305。在我创建了一个函数写随机文件,代码可以参考。在
请注意第
list= random.sample(range(1, 306), 220)
行中的220
。在粘贴完成后,您可以编写代码并更改路径
^{pr2}$比运行代码,错误信息将收入。在
将
list= random.sample(range(1, 306), 220)
行中的数字从220
更改为305
之后,错误将消失。在完整的代码。在
请参考。在
我查了你的密码,是对的。 那么问题可能是: 1您只能运行一次代码,两次或更多次将导致此错误。 2在运行代码之前,请确保所有306文件都在小麦目录中。在
我建议使用复制,而不是移动,然后在每次运行之前清除列车和测试文件。在
相关问题 更多 >
编程相关推荐