2016-01-20 124 views
1

我需要使用修補程序目錄中提供的文件更新現有目錄。 這就是我開始。所有評論都由我發表,然後我嘗試構建每一行。查找,重命名和替換文件

# $SourceDirectory = Patch folder that has files in any number of sub folders 
# $DestDirectory = Application folder that has the files that need patching 
# $UnMatchedFilesFolder = A Folder where SourceFiles go that don't have a match in $DestDirectory 
# import os.path 
# import os.listdir 
# 
# Create list1 of files from $SourceDirectory 
# For each file (excluding directory names) in List1 (including subfolders), search for it in $DestDirectory and its subfolders; 
# If you find the file by the same name, then create a backup of that file with .old; 
# move $DestDirectoryPathAndFile to $DestDirectoryPathAndFile.old; 
# print "Creating backup of file"; 
# After the backup is made, then copy the file from the $SourceDirectory to the; 
# exact same location where it was found in the $DestDirectory. ; 
# Else; 
# move file to UnmatchedFilesDirectory.; 
# If the number of files in $UnMatchedFilesDirectory =/ 0; 
# Create list3 from $UnmatchedFilesDirectory 
# print "The following files in $UnMatchedFilesDirectory will need to be installed individually"; 
# Print "Automated Patching completed."; 

# Print "Script completed"; 
+3

這看起來像僞代碼,而不是Python的?可能想要編輯它,以清楚它意味着什麼是評論和什麼不是。如果2周後您無法分辨僞代碼和真實代碼的差異,那麼這個課程就浪費您的時間。 –

+0

你有什麼嘗試?你能給我們提供你寫的代碼的例子,它打破了什麼,你期望的是什麼?我們都可以爲你編寫代碼,但如果你正在嘗試學習python,那不會太有用。 –

+0

是的,這是我剛剛手寫的psuedo代碼,可以獲得一些粗略的想法。它決不是形式或形式是真實的代碼。我睡了一小會兒,現在我要再來一次。敬請關注。 –

回答

1

正如前一篇文章中提到的那樣,我基於給出的信息對您所關注的課程持懷疑態度。根據所給出的文檔,可以免費獲得更好的網站/教程,以幫助您學習Python /編程。這就是說,Stack Overflow是一個友好的地方,所以我希望能爲您提供信息,這將有助於你的方式:

import os 
source_dir =r"D:\temp" 
dest_dir=r"D:\temp2" 

for root, dirs, files in os.walk(source_dir): 
    # os.walk 'root' steps through subdirectories as we iterate 
    # this allows us to join 'root' and 'file' without missing any sub-directories 
    for file in files: 
     exist_path = os.path.join(root, file) 
     # expected_file represents the fullpath of a file we are looking to create/replace 
     expected_file = exist_path.replace(source_dir, dest_dir) 
     current = os.path.join(root, file) 
     if os.path.exists(expected_file): 
      print "The file %s exists, os.rename with '.old' before copying %s" % (current, exist_path) 
      # .. note:: we should rename to .bkp here, then we would correctly copy the file below without conflict 
     print "Now %s doesn't exist, we are free to copy %s" % (expected_file, exist_path)