2014-11-07 62 views
-1

我试图创建多个子目录和文件移动到这些子目录,子目录下的文件的是哪个环路的数量,这是我所:创建多个目录

for x in range(1,20): 
    os.makedirs('{}/'.format(replace)+str(x)+'/') 
    shutil.move(filename,'{}/'.format(replace)+str(x)+'/') 
    shutil.move(filename1,'{}/'.format(replace)+str(x)+'/') 

我得到这个错误:

File "testdraft.py", line 285, in findReplace 
    shutil.move(f, '{}/'.format(replace)+str(x)+'/') 
    File "/usr/lib/python2.7/shutil.py", line 284, in move 
    if _samefile(src, dst): 
    File "/usr/lib/python2.7/shutil.py", line 58, in _samefile 
    return os.path.samefile(src, dst) 
    File "/usr/lib/python2.7/posixpath.py", line 162, in samefile 
    s1 = os.stat(f1) 
TypeError: coercing to Unicode: need string or buffer, file found 

感谢您的帮助

+0

请告诉我们什么' f'要么打印出它的'repr',要么显示产生它的代码,或者理想的是两者。否则,这不是[完整示例](http://stackoverflow.com/help.mcve),只能通过猜测来回答。 – abarnert 2014-11-07 01:34:48

回答

0

想必你f文件对象,而不是文件名。

您实际上未向我们展示任何代码,其调用shutil.movef;你反而向我们展示了一些代码,这些代码用名为filename1的东西调用它。但是,变量的名称并不重要;如果它返回的文件对象(例如open函数)存在,则不能将其与move一起使用。

希望,你真正的代码是一样的东西一样简单:

with open(out_path, 'w') as f: 
    write_data(f) 
shutil.move(f, '{}/'.format(replace)+str(x)+'/') 

然后你只是想改变f在最后一行out_path,就大功告成了。

0

它看起来像你的文件名或filename1变量实际上不是文件名,而是文件对象。 (这是什么,“文件中找到”在你的错误结束时想告诉你。)

此外,你可能要考虑不首先使目录:

shutil.move(src, dst):

The destination directory must not already exist. If the destination already exists but is not a directory, it may be overwritten depending on os.rename() semantics.