我有一個圖像目錄。其中一些圖像必須存儲在文本文件中,如'pic1.jpg'讀取文本文件並將文件從其移動到目錄
我需要提取此文件名,從當前工作目錄中選取匹配文件並將其移至單獨的文件夾(在cwd下) 。
這是我到目前爲止的代碼,但我不能讓shutil操作工作。我究竟做錯了什麼?
當前目錄C:\ BE
必須從一個名爲 「Filelist.txt中」 至C文本文件移動一個文件(一個或多個)1,JPG,2,JPG等:\ BE \ 2014-03-25_02 -49-11
import os, datetime
import shutil
src = os.getcwd()
global mydir
def filecreation(content, filename):
mydir = os.path.join(os.getcwd(), datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
try:
os.makedirs(mydir)
except OSError, e:
if e.errno != 17:
raise # This was not a "directory exist" error..
with open(os.path.join(mydir, filename), 'w') as d:
d.writelines(content)
#shutil.copyfile(src,mydir)
def main():
filelist = "filelist.txt"
with open(filelist) as f:
content = f.read().splitlines()
#content = shutil.copyfile(src, mydir)
print content
print "Here we are"
#list=['1.jpg','2.jpg']
filecreation(content,"filelist.txt")
print "lets try another method"
with open('filelist.txt','w+') as list_f:
for filename in list_f:
with open(filename) as f:
content = f.read()
#content = shutil.move(src,mydir)
#content = shutil.copyfile(src,mydir)
#for line in f
print "method 2 is working so far"
if __name__ == '__main__':
main()
如果您遇到任何異常,張貼堆棧跟蹤中的問題 –
我找到了解決方案。 – boltthrower