2017-10-01 14 views
1

我打算將當前目錄('〜/ Documents /')中的文件移動到目標文件'〜/ Desktop'a.pyb.pyc.py使用'管道'以交互模式移動文件

import os 
import glob 
path = '~/Documents/' 
os.chdir(path) 
destination_path = '~/Desktop' 

下一步實現

file = glob.glob(path + '*.py') 
source_files = file[:] 

設置命令

cmd = 'mv %s %s' 

迭代文件

文件
for file in source_files: 
    os.open(cmd %(file, destination_path)) 

錯誤報告

Traceback (most recent call last): 
    File "move-files.py", line 14, in <module> 
    os.open(cmd %(file, destination_path)) 
TypeError: Required argument 'flags' (pos 2) not found 

我試着申請evalcmd和其他操作來解決這個錯誤。 如何用「管道」大量移動文件?

+0

你想做什麼?只需使用「mv」移動文件? – paperazzo79

+0

是的,發現錯誤,用os.oppen誤用os.popen。 @ paperazzo79 –

+0

我建議使用'os.rename'或'shutil.move'而不是'os.open'。它們使用起來更安全。 – pschill

回答

0
for file in source_files: 
    os.open(cmd %(file, destination_path)) # misuse os.popen()