Zip Error message if there is a space in the windows directory name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guitonoklops9
    New Member
    • Jun 2007
    • 2

    Zip Error message if there is a space in the windows directory name

    I am very new to python and programming. Right now I'm working on this simple backup program:

    Code:
    #C:\python25\programs\
    # File name: test
    import os, time
    source = ['C:/test/']
    target_directory = 'C:/Backup/'
    today = target_directory + time.strftime('%m.%d.%Y')
    now = time.strftime('BackUp@_%H.%M.%S')
    if not os.path.exists(today):
    	os.mkdir(today)
    	print 'Successfully created directory', today
    target = os.path.join(today, now + '.zip')
    zip_command = "zip -qrv %s %s" % (target, '' .join(source))
    print zip_command
    if os.system(zip_command) == 0:
    	print 'sucessful backup to', target
    else:
    	print 'Backup FAILED'
    This code woks fine. If I try to use a directory with spaces in the path on line 4 like:

    Code:
    source = ['C:/test/test 2/']
    I get a zip error:

    zip warning: name not matched: C:/test/test
    zip warning: name not matched: 2/
    zip error: Nothing to do!

    I know that my syntax is probably not right and that I probably do not have quotes in the right spot (maybe in the zip_command on line 12). I have tried a lot of different possibilities and cannot seem to get the qoutes in the right spot. Any help or pointers would be greatly appreciated!!!
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by guitonoklops9
    I am very new to python and programming. Right now I'm working on this simple backup program:

    Code:
    #C:\python25\programs\
    # File name: test
    import os, time
    source = ['C:/test/']
    target_directory = 'C:/Backup/'
    today = target_directory + time.strftime('%m.%d.%Y')
    now = time.strftime('BackUp@_%H.%M.%S')
    if not os.path.exists(today):
    	os.mkdir(today)
    	print 'Successfully created directory', today
    target = os.path.join(today, now + '.zip')
    zip_command = "zip -qrv %s %s" % (target, '' .join(source))
    print zip_command
    if os.system(zip_command) == 0:
    	print 'sucessful backup to', target
    else:
    	print 'Backup FAILED'
    This code woks fine. If I try to use a directory with spaces in the path on line 4 like:

    Code:
    source = ['C:/test/test 2/']
    I get a zip error:

    zip warning: name not matched: C:/test/test
    zip warning: name not matched: 2/
    zip error: Nothing to do!

    I know that my syntax is probably not right and that I probably do not have quotes in the right spot (maybe in the zip_command on line 12). I have tried a lot of different possibilities and cannot seem to get the qoutes in the right spot. Any help or pointers would be greatly appreciated!!!
    You'll want to use a technique that puts quotes around the directory name for you. Like this:

    >>> print "'hello world'" # single quotes inside quotes
    'hello world'
    >>> print repr("hello world")
    'hello world'
    >>>

    So in your case it might be[CODE=python]
    zip_command = "zip -qrv %s %s" % (repr(target), '' .join(repr(sour ce)))
    [/CODE]But I haven't tested that.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Originally posted by guitonoklops9
      I am very new to python and programming. Right now I'm working on this simple backup program:

      Code:
      #C:\python25\programs\
      # File name: test
      import os, time
      source = ['C:/test/']
      target_directory = 'C:/Backup/'
      today = target_directory + time.strftime('%m.%d.%Y')
      now = time.strftime('BackUp@_%H.%M.%S')
      if not os.path.exists(today):
      	os.mkdir(today)
      	print 'Successfully created directory', today
      target = os.path.join(today, now + '.zip')
      zip_command = "zip -qrv %s %s" % (target, '' .join(source))
      print zip_command
      if os.system(zip_command) == 0:
      	print 'sucessful backup to', target
      else:
      	print 'Backup FAILED'
      This code woks fine. If I try to use a directory with spaces in the path on line 4 like:

      Code:
      source = ['C:/test/test 2/']
      I get a zip error:

      zip warning: name not matched: C:/test/test
      zip warning: name not matched: 2/
      zip error: Nothing to do!

      I know that my syntax is probably not right and that I probably do not have quotes in the right spot (maybe in the zip_command on line 12). I have tried a lot of different possibilities and cannot seem to get the qoutes in the right spot. Any help or pointers would be greatly appreciated!!!
      I cannot explain the problem you are having, but you should consider making your backups with the zipfile module instead of executing a system command. Check out this thread: http://www.thescripts.com/forum/thread600628.html

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        Originally posted by guitonoklops9
        Code:
        source = ['C:/test/test 2/']
        source=os.path. join("c:\\","te st","test\ 2")

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by ghostdog74
          source=os.path. join("c:\\","te st","test\ 2")
          Yields
          'c:\\test\\test \\ 2'
          which doesn't look quite right to me. But I do need to start using that module so that I can give expamples like yours, gd.

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #6
            Originally posted by bartonc
            Yields
            'c:\\test\\test \\ 2'
            which doesn't look quite right to me. But I do need to start using that module so that I can give expamples like yours, gd.
            hi bc, the extra \ is to escape the white space for that directory with space.
            os.path.join will take care of that. I have tested it on my Win32 system, using putty.exe ( chosen randomly ) which is stored in my "Program Files" directory, and then using os.system(). Putty started successfully, so i am quite sure it works. however, if it doesn't work for the majority, i think something is wrong with my system lol :)

            Comment

            • Motoma
              Recognized Expert Specialist
              • Jan 2007
              • 3236

              #7
              Originally posted by ghostdog74
              hi bc, the extra \ is to escape the white space for that directory with space.
              os.path.join will take care of that. I have tested it on my Win32 system, using putty.exe ( chosen randomly ) which is stored in my "Program Files" directory, and then using os.system(). Putty started successfully, so i am quite sure it works. however, if it doesn't work for the majority, i think something is wrong with my system lol :)
              I think he is right and you made a mistake: the code I believe he wants is:

              source=os.path. join("c:\\","te st","test 2")

              You had escaped test 2 before passing it to join, which causes a double escape.

              Comment

              • ghostdog74
                Recognized Expert Contributor
                • Apr 2006
                • 511

                #8
                Originally posted by Motoma
                I think he is right and you made a mistake: the code I believe he wants is:

                source=os.path. join("c:\\","te st","test 2")

                You had escaped test 2 before passing it to join, which causes a double escape.
                here's a demonstration
                Code:
                >>> import os
                >>> s=os.path.join("/home","test","directory with spaces")
                >>> print s
                /home/test/directory with spaces
                >>> os.chdir(s)
                >>> os.getcwd()
                '/home/test/directory with spaces'
                >>> s=os.path.join("/home","test","directory with spaces","test1.sh") #trying to execute a script
                >>> os.system(s)
                sh: /home/test/directory: No such file or directory
                32512
                >>> s=os.path.join("/home","test","directory\ with\ spaces","test1.sh")
                >>> os.system(s)
                0
                0
                0
                0
                0
                it works properly when the spaces are escaped, just like what is done by the shell.

                Comment

                • Motoma
                  Recognized Expert Specialist
                  • Jan 2007
                  • 3236

                  #9
                  Oh man...Are we going to have a code off?

                  [code=python]
                  >>> import os
                  >>> s = os.path.join("C :\\", "Program Files")
                  >>> os.chdir(s)
                  >>> os.getcwd()
                  'C:\\Program Files'
                  >>> s = os.path.join("C :/", "Program Files")
                  >>> os.chdir(s)
                  >>> os.getcwd()
                  'C:\\Program Files'
                  >>> s = os.path.join("C :\\", "Program\ Files")
                  >>> os.chdir(s)

                  Traceback (most recent call last):
                  File "<pyshell#2 6>", line 1, in -toplevel-
                  os.chdir(s)
                  OSError: [Errno 2] No such file or directory: 'C:\\Program\\ Files'
                  >>>
                  [/code]

                  I guess this is a difference between Python on Windows and Linux.

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #10
                    Originally posted by Motoma
                    I guess this is a difference between Python on Windows and Linux.
                    Now we are getting to the heart of the matter. I tend to forget that not everybody uses the same platform.

                    Comment

                    Working...