Error message if there is a space in the source directory

  • Thread starter lukefrancomusic
  • Start date
L

lukefrancomusic

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\BackUp@_10.03.57.zip . -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\programs\
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_directory = 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_directory + 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(today, 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_command) == 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
 
M

Mark Peters

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
 
P

Peter Otten

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\BackUp@_10.03.57.zip . -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\programs\
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_directory = 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_directory + 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(today, 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_command) == 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.07.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\test" 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
 
L

lukefrancomusic

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top