0

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:

  1. 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)
  1. 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.

  2. 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?

  • Go have a look at this link: It has a lot of information that should help you out. https://stackoverflow.com/questions/11574257/how-do-i-write-log-messages-to-a-log-file-and-the-console-at-the-same-time/11581118 Also look at: http://docs.python.org/library/logging.html – Dr t Jun 27 '17 at 18:22
  • I think I need something much more basic than a logging module for that file to be honest; that text file doesn't need to hold more than 1 path at a time – Claudiu Dragan Jun 27 '17 at 18:32

2 Answers2

0

You can use os.system() function by importing it from os. It will execute command in cmd/shell, but for the sub-process only. I hope this is helpfull

JAPSIMRAN
  • 31
  • 7
  • I'm currently importing: import os, errno, sys, os.path. But I don't understand what to do with the os.system() function in my case, can you please elaborate a little? – Claudiu Dragan Jun 27 '17 at 18:53
  • i use linux ,so i will give you an example in it ->os.system("ls") it will make command ls work in the terminal,my point is it may work work similar in cmd – JAPSIMRAN Jun 27 '17 at 19:43
  • I'm sorry, I still don't understand, how does this apply to my question? – Claudiu Dragan Jun 27 '17 at 19:59
  • **in the function**, you create. open(execute) a xyz.py file which wil restore the file to it's original place – JAPSIMRAN Jun 28 '17 at 14:00
0

Alright, so I managed to do this by myself in the end. In case anyone is interested you'll find samples of the code below. It's very rudimentary and can of course be optimized but it does work.

Main:

def Main():

dir_name = input("Enter the destination path: ")
if os.path.isdir(dir_name):
    print ("Directory already existed.")
else:
    print ("Directory created successfully!")
os.makedirs(dir_name)

choice = input("Would you like to (M)ove or (R)estore?: ")

if choice == 'M':
    path = input("Directory of file you want moved: ")
    file = input("Name of the file+extension: ")
    file_path = path+'/'+file
    move(file_path)
    print ("Done.")

elif choice == 'R':
    with open('quar_id.txt') as f:
        quar_id = f.readline()
    restore_from_quar(quar_id)
    print ("Done.")

else: 
    print ("No valid option selected, closing...")

Move:

def move(filepath):

f = open('quar_id.txt','w')
f.write(path)
f.close()
os.chdir(dir_name)
shutil.move(file_path,dir_name+'/'+file)

Restore:

def restore(quar_id):

os.chdir(dir_name)
myfile = os.listdir(dir_name)
file = str(myfile)
file = file[2:-2]
shutil.move(file,quar_id+'/'+file)