0
我一直在試圖編寫一個程序來從文檔/ VisualStudio2008文件夾中獲取所有.cpp文件,但我無法將它們複製到桌面。我希望它將所有.cpp從指定的文件夾移動到桌面。我有一個標準化的命名方案,我已經做了我能想到的所有事情。任何幫助表示讚賞。代碼如下,如果您需要更多信息,請詢問。在python中複製文件的問題
import shutil, os
src = ""
dest = ""
chapter = 0
type = ["StepByStep", "Project", "Activity"]
currentType = 0
chapter = int (input("What chapter files would you like to get?\n>> "))
destFolder = input("What do you want the folder name to be?\n>> ")
dest = "C:/Users/17haydent/desktop/"
src = "C:/Users/17haydent/Documents/Visual Studio 2008/Projects/"
#srcEnd = name/name/name.cpp
def upType():
global currentType
if currentType == 0:
currentType = 1
elif currentType == 1:
currentType = 2
dest += destFolder
for a in range(1, 3):
for b in range(1, 20):
newSrc = src + type[currentType] + str(chapter) + '_' + str(b) + '/' + type[currentType] + str(chapter) + '_' + str(b) + '/' + type[currentType] + str(chapter) + '_' + str(b) + ".cpp"
newDest = dest + '/' + type[currentType] + str(chapter) + '_' + str(b) + ".cpp"
try:
assert not os.path.isabs(newSrc)
dstdir = os.path.join(newDest, os.path.dirname(newSrc))
os.makedirs(dstdir) # create all directories, raise an error if it already exists
shutil.copyfile(newSrc, dstdir)
except:
print("Error copying file", newSrc, "to", newDest)
upType()
您介紹了您的場景並給出了代碼。但是問題是什麼?什麼不按預期工作? – Lovato 2014-09-24 13:51:59