471,337 Members | 867 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

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:

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
9 11410
bartonc
6,596 Expert 4TB
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
2,851 Expert Mod 2GB
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
ghostdog74
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. source = ['C:/test/test 2/']
source=os.path.join("c:\\","test","test\ 2")
Jun 3 '07 #4
bartonc
6,596 Expert 4TB
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
ghostdog74
511 Expert 256MB
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
3,237 Expert 2GB
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
ghostdog74
511 Expert 256MB
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
3,237 Expert 2GB
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
6,596 Expert 4TB
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.

Similar topics

reply views Thread by Nikola Skoric | last post: by
2 posts views Thread by amita | last post: by
1 post views Thread by Bruce | last post: by
5 posts views Thread by Eric Davidson | last post: by
1 post views Thread by David Greenberg | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.