Error message if there is a space in the source directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lukefrancomusic@gmail.com

    #1

    Error message if there is a space in the source directory

    I am trying to learn Python. I am working on a simple backup program
    (code listed below). When using a source directory (the files to be
    backed up) without spaces in the title, my program works fine [see
    line 5]. If I try to access a directory with a space in the name the
    program fails with this error message:

    zip error:Nothing to do! (try: zip -qr C:\Backup\
    \06.02.2007\Bac kUp@_10.03.57.z ip . -i C:\test\test2\\ )
    Backup FAILED

    I've been trying to find the answer for a while now but am stumped and
    don't know exactly what to look for. Any help would be greatly
    appreciated!


    1. # C:\python25\pro grams\
    2. # File name: backup3debug.py
    3. import os, time
    4. # 1. The files and directories to be backed up are specified in
    a list.
    5. source = [r'C:\test\test2 \\']
    6. # 2. The backup must be stored in a main backup directory.
    7. target_director y = r'C:\Backup\\'
    8. # 3. The files are backed up into a zip file.
    9. # 4. The name of the directory is the current date.
    10. today = target_director y + time.strftime(' %m.%d.%Y')
    11. # The current time is the name of the zip archive.
    12. now = time.strftime(' BackUp@_%H.%M.% S')
    13. # Create the subdirectory if it does not exist already.
    14. if not os.path.exists( today):
    15. os.mkdir(today)
    16. print 'Successfully created directory', today
    17. # The name of the zip file.
    18. target = os.path.join(to day, now + '.zip')
    19. # 5. We use the standard ''zip'' command to put the files in a
    zip archive.
    20. zip_command = "zip -qr %s %s" % (target, ' '.join(source))
    21. print zip_command
    22. # Run the backup
    23. if os.system(zip_c ommand) == 0:
    24. print 'sucessful backup to', target
    25. else:
    26. print 'Backup FAILED'

    When using a source like this on line 5:

    source = [r'C:\test\test 2\\']

    which has a space in the title, the program will not work.
    =============== =============== =============== =======
    Windows XP sp2
    Dell Latitude D600

  • Mark Peters

    #2
    Re: Error message if there is a space in the source directory

    When using a source like this on line 5:
    >
    source = [r'C:\test\test 2\\']
    >
    which has a space in the title, the program will not work.
    Try wrapping that argument in double quotes when you build the command

    Comment

    • Peter Otten

      #3
      Re: Error message if there is a space in the source directory

      lukefrancomusic @gmail.com wrote:
      I am trying to learn Python. I am working on a simple backup program
      (code listed below). When using a source directory (the files to be
      backed up) without spaces in the title, my program works fine [see
      line 5]. If I try to access a directory with a space in the name the
      program fails with this error message:
      >
      zip error:Nothing to do! (try: zip -qr C:\Backup\
      \06.02.2007\Bac kUp@_10.03.57.z ip . -i C:\test\test2\\ )
      Backup FAILED
      >
      I've been trying to find the answer for a while now but am stumped and
      don't know exactly what to look for. Any help would be greatly
      appreciated!
      >
      >
      1. # C:\python25\pro grams\
      2. # File name: backup3debug.py
      3. import os, time
      4. # 1. The files and directories to be backed up are specified in
      a list.
      5. source = [r'C:\test\test2 \\']
      6. # 2. The backup must be stored in a main backup directory.
      7. target_director y = r'C:\Backup\\'
      8. # 3. The files are backed up into a zip file.
      9. # 4. The name of the directory is the current date.
      10. today = target_director y + time.strftime(' %m.%d.%Y')
      11. # The current time is the name of the zip archive.
      12. now = time.strftime(' BackUp@_%H.%M.% S')
      13. # Create the subdirectory if it does not exist already.
      14. if not os.path.exists( today):
      15. os.mkdir(today)
      16. print 'Successfully created directory', today
      17. # The name of the zip file.
      18. target = os.path.join(to day, now + '.zip')
      19. # 5. We use the standard ''zip'' command to put the files in a
      zip archive.
      20. zip_command = "zip -qr %s %s" % (target, ' '.join(source))
      21. print zip_command
      22. # Run the backup
      23. if os.system(zip_c ommand) == 0:
      24. print 'sucessful backup to', target
      25. else:
      26. print 'Backup FAILED'
      I realize that you have good intentions, but this script is commented to
      death. Make a guess which points a reader may stumble over and only comment
      these.
      When using a source like this on line 5:
      >
      source = [r'C:\test\test 2\\']
      >
      which has a space in the title, the program will not work.
      That's because in the line

      zip -qr C:\Backup\\06.0 7.2008\BackUp@_ 01.02.03.zip C:\test\test 2\\

      the shell (or whatever split the above line into separate arguments) has no
      way of knowing that "C:\test\te st" and "2\\" are intended to be one
      argument. Use subprocess.call () instead of os.system(). You can pass it a
      list an thus avoid the ambiguity:

      zip_command = ["zip", "-qr", target] + source
      subprocess.call (zip_command)

      Peter

      Comment

      • lukefrancomusic@gmail.com

        #4
        Re: Error message if there is a space in the source directory

        Wow, great!! That worked like a charm. Sorry about the excessive
        commenting, when looking it over I could see how that would be
        annoying. Thank you Peter!

        Luke

        Comment

        Working...