1

I am writing a simple python script that connects to several remote Windows machines, read the content of a remote folder on these machine, and then zip and copy all files that have been modified after a given date.

The problem is that after it connects to the first computer, it does not connect to the second computer, the "net use" command does not work. If I do it manually though the windows command line of my computer, it does work, but not through my python script.

I have not found any topic that could help me, and I am kind of stuck now... Have you guys any idea of what I could do wrong ?

Below is my code (I apologize if it is not looking very neat, I am starting in python).

import os, subprocess, datetime, shutil


# the local destination of log files on my computer
ROOT_folder = 'C:\\Logs'

for i, IP in enumerate(list_of_IPs):

    # list of names corresponding to the IPs
    location = list_of_locs[i]

    # create a local repository in my ROT folder for storing the logs of this remote station
    try:
        os.chdir(ROOT_folder + '\\' + location)
    except:
        os.mkdir(ROOT_folder + '\\' + location)

    # The path to the logs that are stored on the remote windows machines
    remote_path_to_logs = '_Temporary\\Logs'

    print location

    # Mapping a drive m:      >>> Here I get the error at the 2nd iteration
    subprocess.call(r'net use m: \\' + IP + '\c$ Password /user:Username', shell=True)    

    # The modification date of the most recent file I donwloaded is stored on my local computer in a txt file - here I read the date
    try:
        with open(ROOT_folder + '\\' + location + '\\last_mod.txt','r') as myFile:
            last_file_downloaded = datetime.datetime.strptime(myFile.read(),'%Y-%m-%d %H:%M:%S')             
    except:
        last_file_downloaded = datetime.datetime(1970,1,1)

    os.chdir('M:\\' + remote_path_to_logs)

    # I sort the list of files from oldest to newest
    list_files = os.listdir('M:\\' + remote_path_to_logs)
    list_sorted = sorted([(fl, os.path.getmtime(fl)) for fl in list_files],key=lambda x: x[1])

    for log, logtime in list_sorted:
        date_file = datetime.datetime(1970,1,1) + datetime.timedelta(seconds=logtime)

        # I zip and move the file to my computer if it was modified after the date I stored on my computer
        if date_file > last_file_downloaded :
            print log + ': zipping and moving to local directory... '
            with zipfile.ZipFile(date_file.strftime('%Y-%m-%d_%H-%M-%S') + '.zip','w', zipfile.ZIP_DEFLATED) as z:
                z.write(log)
            shutil.move(date_file.strftime('%Y-%m-%d_%H-%M-%S') + '.zip',ROOT_folder + '\\' + location)

            # I overwrite the modification date in my file
            with open(ROOT_folder + '\\' + location + '\\last_mod.txt','w') as myFile:
                myFile.write(date_file.strftime('%Y-%m-%d %H:%M:%S'))     


    # Disconnecting the drive m:
    subprocess.call('net use m: /delete /yes', shell=True)
    # I tried to put a time.sleep(5) here but it does not help
user3722440
  • 243
  • 1
  • 6
  • 14
  • 1
    Have you tried using `os.system()`, instead of `subprocess.call()`? – SiHa Jul 29 '15 at 12:41
  • Thanks Siha. Yes, tried that and no success. The first iteration works perfectly, i.e. a drive is mapped for the first remote machine, the log file is copied on my local machine, and the drive is disconnected. The error occurs at the second iteration... – user3722440 Jul 29 '15 at 16:56
  • 1
    I'd try using different drive letters for a couple of iterations. That will show if the problem is the drive letter not being released in time. – SiHa Jul 29 '15 at 17:10
  • Thanks. Actually, if I don't use a letter at all for the mapped drive, the script is working (I use the command 'net use \\' + IP + '\c$ Password /user:Username' instead of 'net use m: \\' + IP + '\c$ Password /user:Username'. It works, althgough I still can't explain why it is not working using the same letter for the mapped drive. – user3722440 Jul 29 '15 at 19:31
  • If that works for you, I'd post it an an answer, because it could potentially be very useful to others. I certainly didn't know you could not specify a drive letter and simply use the UNC path instead. – SiHa Jul 30 '15 at 07:14
  • I've found that net use takes a few seconds before producing reliable responses (e.g. os.path.isdir will fail immediately after a net use, but will work several seconds later). – Tahlor Jun 24 '17 at 04:23

1 Answers1

0

Actually, if I don't use a letter at all for the mapped drive, the script is working: I use the command 'net use \\' + IP + '\c$ Password /user:Username' instead of 'net use m: \\' + IP + '\c$ Password /user:Username'.

It works, althgough I still can't explain why it is not working using a same letter.

user3722440
  • 243
  • 1
  • 6
  • 14