2013-10-07 51 views
0

在Python上,我需要帮助搜索具有特定标题类型(PST文件,标题序列21 42 44 4E)的文件,然后将它们复制到我保存的文件目录中。基于标题的Python复制文件

以下是我的代码的相关摘录。

# get working directory of my program 
ori_path=os.getcwd() 
# this is where the file is saved(from the root directory of my program) 
temp = "\etc\Saved Files" 
# obtain value written in textbox, the path to search 
path_to_copy_from=self.textbox.GetValue() 
# create the absolute destination path so that the files don't end up somewhere they shouldn't be 
copy_path="{}{}".format(ori_path,temp)  
# change working directory to the one specified by user 
os.chdir(path_to_copy_from) 

我将使用shutil做像这样的复制:

shutil.copy(files,copy_path) 

一些搜索,我发现使用itertools提及,但我不明白的例子(因此,为什么我是问题)。我需要帮助来找到将查看文件头的代码,然后在头与PST头格式匹配时调用shutil。

回答

0

为了获得前4个字节的文件从你的问题的十六进制格式:

header = "" 
with open(path_to_copy_from, 'rb') as f: 
    for i in range(4): 
     byte = f.read(1) 
     header += hex(ord(byte))[2:] + " " 

然后,您可以检查该文件夹中的每个文件这header串是否符合您正在寻找一。

+1

感谢@Junuxx等。你的代码专门针对一个文件,但我提供的路径是一个目录。但是,通过使用glob我可以在目录中的文件上运行此代码。非常喜欢 – KishanPD

0

与junuxx的解决方案,我找出了一种方法来做到这一点的目录中的所有文件。最终的代码看起来这

 import wx #this is for GUI interfaces i made to interact with the user. aka wxPython 
    import os 
    import glob 
    import shutil 
    #the above are the needed libraries. 
    path_to_copy_from=self.textbox.GetValue() #obtain value written in textbox(made with wxPython) 
    if os.path.exists(path_to_copy_from): 
     ori_path=os.getcwd()#get working directory of my program 
     #combine ori_path and temp to create the destination path for files to be copied to 
     copy_path="{}{}".format(ori_path,temp) 
     #change working directory to the one specified by user 
     os.chdir(path_to_copy_from) 
      #to copy files based on header 
      header = "" 
      pst_header="21 42 44 4e " 
      for self.files in glob.glob("*.*"): 
       try: 
        with open(self.files, 'rb') as f: 
         for i in range(4): 
          byte = f.read(1) 
          header += hex(ord(byte))[2:] + " " 

         if header == pst_header: 
          shutil.copy(self.files,copy_path) 
          #the following 2 lines tells the user using a textbox i made earlier that something is happening. made for a textctrl i made with wxPython 
          self.textbox2.AppendText("Found file with .pst header.\n") 
          self.textbox2.AppendText("Copied {} to {}. \n".format(self.files,copy_path)) 
          #to change copied file to read only 
          path_to_file="{}\{}".format(copy_path,self.files) 
          #set the file as read-only(for my program only, not necessary to have) 
          os.chmod(path_to_file,0444) 
         #to remove the string already in header for next iteration 
         header = "" 
       #simple exception handling. change as you need 
       except TypeError, ex: 
        pass 
       except IOError, ex: 
        pass 

非常感谢:)