2011-10-31 38 views
0

我需要創建一個不存在的子目錄,然後將一些文件複製到其中。然而,每當我嘗試時,我都會得到一個Permission denied錯誤。我試過chmod,777以及stat.S_IWRITE,我試過os.system('attrib -r),但沒有任何效果。任何人都可以幫我解決這個問題嗎?我知道網站上有類似的問題,但它說使用chmod,這不適合我。使用Python在Windows中創建可寫目錄os.makedirs

這裏是我的代碼:

beginpath = "C:\Users\foo" 
fullpath = os.path.join(beginpath, foldername) 
print fullpath 
fullpath = fullpath.replace('\n', '') 

##create a folder to hold the deleted files 
deleted = os.path.join(fullpath, "Deleted") 
print deleted 
if not os.path.exists(deleted): 
      os.makedirs(deleted) 
      os.chmod(deleted, stat.S_IWRITE) 
      print "created" 



##do some other processing here 


oldfile = os.path.join(fullpath, newpagename) 
shutil.copyfile(oldfile, deleted) 
+0

如果你喜歡你得到的答案,請檢查它,以便這個傢伙得到點數! –

回答

1

我認爲shutil.copyfile需要的目標文件的完整文件名,而不僅僅是目錄。

所以

shutil.copyfile(oldfile, os.path.join(deleted, newpagename)) 

應該做的伎倆。

+0

工作正常!非常感謝! – bsg

+0

...或使用'shutil.copy(oldfile,刪除)' – Marvin