#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    New to python need help with practice


    Hello! Sorry to bother you folks with something that probably isn't very exciting, but it would really help me if anyone had a quick solution to my problem. I am working Byte of Python 1.92, and its his problem solving exercise in creating a zip backup of a directory. It looks like this:

    Code:
    import time 
    import os  
    source=['C:\\gs'] 
    target_dir='C:\\backup' target=target_dir + os.sep +time.strftime('%Y%m%d%H%M%S')+'.zip' 
    zip_command="zip -qr {0} {1}".format(target,''.join(source)) 
    if os.system(zip_command) == 0:     
        print('Successful backup to', target) 
    else:     
        print('Backup FAILED')
    So, I understand that the directory that zip.exe is in has to be in the list, sys.path. I have already appended it, and have checked multiple times to make sure it is correct. I have also checked to make sure all proper directories exist. Finally, I have used "zip_command" string in command prompt and it worked perfectly. I am running out of ideas; essentially it seems that even though I have the directory zip.exe is in, I am unable to use it. Also, I have done previous things involving modules and I was able to make use of ones in the default directories (the current directory, ' ', mainly). Is there any other problem it could be?

    The message in command prompt when I run in my python interpreter (a little window flashes and leaves, that I captured with a screenshot) says " ' zip' is not recognized as an internal or external command, operable program, or batch file. " Which I have understood to essentially mean that it is not seeing that directory (I have seen this same message when trying to run the command in command prompt in any other directory than the one zip.exe is in). So that leads me to believe that even though it is in sys.path, it is acting as if it isn't.

    This is my sys.path:
    Code:
    ['',
     'C:\\Windows\\system32\\python32.zip',  
    'C:\\Python32\\Lib', 
    'C:\\Python32\\DLLs', 
    'C:\\Python32', 
    'C:\\Python32\\lib\\site-packages', 
    'C:\\Program Files (x86)\\GnuWin32\\bin']
    I appreciate any help, even if its just shared befuddlement. Thanks!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Differences between this post and your post are 1) target= starts on a new line, and I've displayed the command you plan to run via the system function. Would that command work on the "DOS command line"?

    Next, you're a bit confused. Python searches for dynamic libraries and .py files using sys.path .
    The operating system, on the other hand, will search for zip.exe using the PATH in the shell. You can see that (and modify as you would a python dictionary)
    print(os.environ['PATH'])
    Code:
    import time 
    import os  
    source=['C:\\gs'] 
    target_dir='C:\\backup'
    target=target_dir + os.sep +time.strftime('%Y%m%d%H%M%S')+'.zip' 
    zip_command="zip -qr {0} {1}".format(target,''.join(source)) 
    
    print(zip_command)
    
    if os.system(zip_command) == 0:     
        print('Successful backup to', target) 
    else:     
        print('Backup FAILED')
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0
    Ahh I see now, so environmental variables are associated with the subshell. This dictionary does seem weirder than the previous ones I looked at though. I went ahead and added the directory in the control panel actually, through system. Thank you though for clearing up that misconception!

IMN logo majestic logo threadwatch logo seochat tools logo