I'm relatively new to python and I'm working on a few projects. Say I'm running the script on partition D on Windows, so, for example, it's "D:/quarantine.py"
What I'm looking for right now is:
- Taking a file from one folder (say, Desktop) and moving it to another folder (say, C:\Quarantine) - but I need to read both files and directories from the keyboard. The C:\Quarantine folder is created earlier with this code:
def create_quar(quar_folder): try: os.makedirs(quar_folder) except OSError as exception: if exception.errno != errno.EEXIST: raise dir_name = input("Enter the desired quarantine path: ") if os.path.isdir(dir_name): print ("Directory already existed.") else: print ("Directory created successfully!") create_quar(dir_name)
Before moving the file, I need to store the file's previous location somehow. I was thinking of creating a .txt file in that C:\Quarantine folder.
If I ever change my mind, I call on a function that reads the .txt file I created earlier, and just moves the files back to the original folder. This, I have no idea how to implement.
I'm at a loss as to how to go about doing this. Was thinking of something like this for logging the path and moving the file:
path = input("Directory of file I need to move: ")
file = input("File name: ")
f = open(dir_name+"\log.txt",'w')
f.write(os.path.abspath(path+file))
shutil.move(path+file,dir_name+file)
dir_name is the variable I used earlier to read the Quarantine folder location, so I figured I could reuse it. As for the reading the log file and restoring, I have no idea.
Can anyone help?