By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,846 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

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

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. #C:\python25\programs\
  3. # File name: test
  4. import os, time
  5. source = ['C:/test/']
  6. target_directory = 'C:/Backup/'
  7. today = target_directory + time.strftime('%m.%d.%Y')
  8. now = time.strftime('BackUp@_%H.%M.%S')
  9. if not os.path.exists(today):
  10.     os.mkdir(today)
  11.     print 'Successfully created directory', today
  12. target = os.path.join(today, now + '.zip')
  13. zip_command = "zip -qrv %s %s" % (target, '' .join(source))
  14. print zip_command
  15. if os.system(zip_command) == 0:
  16.     print 'sucessful backup to', target
  17. else:
  18.     print 'Backup FAILED'
This code woks fine. If I try to use a directory with spaces in the path on line 4 like:

Expand|Select|Wrap|Line Numbers
  1. 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!!!
Jun 3 '07 #1
Share this Question
Share on Google+
9 Replies


bartonc
Expert 5K+
P: 6,596
I am very new to python and programming. Right now I'm working on this simple backup program:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #C:\python25\programs\
  3. # File name: test
  4. import os, time
  5. source = ['C:/test/']
  6. target_directory = 'C:/Backup/'
  7. today = target_directory + time.strftime('%m.%d.%Y')
  8. now = time.strftime('BackUp@_%H.%M.%S')
  9. if not os.path.exists(today):
  10.     os.mkdir(today)
  11.     print 'Successfully created directory', today
  12. target = os.path.join(today, now + '.zip')
  13. zip_command = "zip -qrv %s %s" % (target, '' .join(source))
  14. print zip_command
  15. if os.system(zip_command) == 0:
  16.     print 'sucessful backup to', target
  17. else:
  18.     print 'Backup FAILED'
This code woks fine. If I try to use a directory with spaces in the path on line 4 like:

Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. zip_command = "zip -qrv %s %s" % (repr(target), '' .join(repr(source)))
  2.  
But I haven't tested that.
Jun 3 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,851
I am very new to python and programming. Right now I'm working on this simple backup program:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #C:\python25\programs\
  3. # File name: test
  4. import os, time
  5. source = ['C:/test/']
  6. target_directory = 'C:/Backup/'
  7. today = target_directory + time.strftime('%m.%d.%Y')
  8. now = time.strftime('BackUp@_%H.%M.%S')
  9. if not os.path.exists(today):
  10.     os.mkdir(today)
  11.     print 'Successfully created directory', today
  12. target = os.path.join(today, now + '.zip')
  13. zip_command = "zip -qrv %s %s" % (target, '' .join(source))
  14. print zip_command
  15. if os.system(zip_command) == 0:
  16.     print 'sucessful backup to', target
  17. else:
  18.     print 'Backup FAILED'
This code woks fine. If I try to use a directory with spaces in the path on line 4 like:

Expand|Select|Wrap|Line Numbers
  1. 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
Jun 3 '07 #3

Expert 100+
P: 511
Expand|Select|Wrap|Line Numbers
  1. source = ['C:/test/test 2/']
source=os.path.join("c:\\","test","test\ 2")
Jun 3 '07 #4

bartonc
Expert 5K+
P: 6,596
source=os.path.join("c:\\","test","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.
Jun 4 '07 #5

Expert 100+
P: 511
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 :)
Jun 4 '07 #6

Motoma
Expert 2.5K+
P: 3,235
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:\\","test","test 2")

You had escaped test 2 before passing it to join, which causes a double escape.
Jun 4 '07 #7

Expert 100+
P: 511
I think he is right and you made a mistake: the code I believe he wants is:

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

You had escaped test 2 before passing it to join, which causes a double escape.
here's a demonstration
Expand|Select|Wrap|Line Numbers
  1. >>> import os
  2. >>> s=os.path.join("/home","test","directory with spaces")
  3. >>> print s
  4. /home/test/directory with spaces
  5. >>> os.chdir(s)
  6. >>> os.getcwd()
  7. '/home/test/directory with spaces'
  8. >>> s=os.path.join("/home","test","directory with spaces","test1.sh") #trying to execute a script
  9. >>> os.system(s)
  10. sh: /home/test/directory: No such file or directory
  11. 32512
  12. >>> s=os.path.join("/home","test","directory\ with\ spaces","test1.sh")
  13. >>> os.system(s)
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
it works properly when the spaces are escaped, just like what is done by the shell.
Jun 5 '07 #8

Motoma
Expert 2.5K+
P: 3,235
Oh man...Are we going to have a code off?

Expand|Select|Wrap|Line Numbers
  1. >>> import os
  2. >>> s = os.path.join("C:\\", "Program Files")
  3. >>> os.chdir(s)
  4. >>> os.getcwd()
  5. 'C:\\Program Files'
  6. >>> s = os.path.join("C:/", "Program Files")
  7. >>> os.chdir(s)
  8. >>> os.getcwd()
  9. 'C:\\Program Files'
  10. >>> s = os.path.join("C:\\", "Program\ Files")
  11. >>> os.chdir(s)
  12.  
  13. Traceback (most recent call last):
  14.   File "<pyshell#26>", line 1, in -toplevel-
  15.     os.chdir(s)
  16. OSError: [Errno 2] No such file or directory: 'C:\\Program\\ Files'
  17. >>> 
  18.  
I guess this is a difference between Python on Windows and Linux.
Jun 5 '07 #9

bartonc
Expert 5K+
P: 6,596
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.
Jun 5 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.