473,390 Members | 981 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 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 11724
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

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

Similar topics

0
by: Nikola Skoric | last post by:
Hello everybody, I have recently migrated from win98+PWS to winXP+IIS, and after that an problem has occured. When I open some of my PHP pages, I get this instead of the page I shlud get: ...
8
by: Glenn A. Harlan | last post by:
Why am I receiving the below error when calling - Path.GetTempFileName() The directory name is invalid. Description: An unhandled exception occurred during the execution of the current web...
3
by: Bryce | last post by:
I need to FTP a file to an FTP server -- the destination path has spaces. When I interactively change directory using FTP in a command window, I can put double quotes around a directory name that...
2
by: amita | last post by:
when i am writing a program in c# using windows app I am getting this error message as System.Windows.Forms.Control.Text is inaccessable due to its protection level. In the design window when i...
1
by: Bruce | last post by:
We suddenly got a mysterious error message on our Windows Server 2003 (SP1) after rolling out a bug fix on a web services application this morning. The application compiles and runs fine on the...
5
by: Eric Davidson | last post by:
I keep getting the folowing error message in my db2diag.log every couple of seconds, and I can not find any information on this on the DB2 website or in the apr's Can anyone give me a pointer...
1
by: David Greenberg | last post by:
Hi In my DTS I am updating an Ingres database on a different server. I'm getting the following error immedtiately .... HResult of 0x80040e09 (-2147217911) returned Unexpected error occured. An...
1
by: ramyamuthusamy | last post by:
hi, I installed a new software called magic folders.. Also i reinstalled it few days back. After the reinstallation an error message occcurs that"windows cannot find magic.exe go to search and...
0
by: Rahul Shakya | last post by:
Hi, I am facing same problem. I have not implmented any dll or no referece at all. In disk usage my control panel showing only (0.2%) of total. I dnt know is my application problem or it is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.