I am new to programing python (5 days now) and have ran into a problem that i can't seem to find the answer to in this forum. i would appreciate any help and a detailed explaination would be very much appreciated.
To being. i'm using python 3 on a macbook pro. i'm using an editor called "komodo edit" I'm using CH Swaroop's "Byte of Python" as a guide.
the problem i'm having is with an example of creating a program that takes a file from one folder and backs it up to another as a zip file.
the example in the book is as follows:
Save as backup_ver1.py:
import os
import time
# The files and directories to be backed up are specified in a list.
source = ['"C:\\My Documents"', 'C:\\Code']
# Notice we had to use double quotes inside the string for names with spaces in it.
# The backup must be stored in a main backup directory
target_dir = 'E:\\Backup' # Remember to change this to what you will be using
# The files are backed up into a zip file.
# The name of the zip archive is the current date and time
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
# We use the zip command to put the files in a zip archive
zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')
my code is as follows
import os
import time
source = ['C:\\learningPython','C:\\scan'] # can i use / instead of the \?
# why is square brackets used here? i'm under the assumption the square brackets are used for list. i'm also assuming one is the directory and the other is the file.
target_dir = 'C:/backup' # created a "backup" folder
target = target_dir + os.sep +time.strftime('%Y%M%D%H%M%S') + '.zip'
zip_command = "zip -qr {0} {1}".format(target, ‚ ‚.join(source))
if os.system(zip_command) == 0:
print(‚Successful backup to‚, target)
else:
print(‚Backup FAILED‚)
I get a
zip error: Nothing to do! (try: zip -qr C:/backup/20133201/15/13203219.zip . -i C:learningPythonC:scan) backup failed
The example code you're looking at is totally Windows specific, what with the C:\blah
paths. An equivalent path on OS X would look something like:
/Users/yourusername/Documents
(You also seem to have somehow transformed a few '
s into ,
s when converting the code. Not sure what that's all about.)