���� Python �̳�
��10�� ������⡪����дһ��Python�ű�
��һҳ ������� ��һҳ

�������

�����ǻ�����ɳ������ƣ����ǾͿ��Ա�д�����ˣ����Ƕ����ǵĽ��������ʵʩ��

�汾һ

��10.1 ���ݽű������汾һ

#!/usr/bin/python
# Filename: backup_ver1.py


import os
import time

# 1. The files and directories to be backed up are specified in a list.
source = ['/home/swaroop/byte', '/home/swaroop/bin']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that

# 2. The backup must be stored in a main backup directory

target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using

# 3. The files are backed up into a zip file.
# 4. The name of the zip archive is the current date and time

target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

# Run the backup
if os.system(zip_command) == 0:
    print 'Successful backup to', target
else:
    print 'Backup FAILED'

��Դ�ļ���code/backup_ver1.py��

���

$ python backup_ver1.py
Successful backup to /mnt/e/backup/20041208073244.zip

���ڣ������Ѿ��������������ˣ���������ڣ����Dz������ǵij����Ƿ���ȷ������������������������IJ�һ�������Ǿ͵��������ǵij��򣬼����������е� 覴� �����󣩡�

������

�������㽫����������ΰ� ��� һ��һ����ת��Ϊ ���� ��

����ʹ����os��timeģ�飬���������������ǡ�Ȼ��������source�б���ָ����Ҫ���ݵ��ļ���Ŀ¼��Ŀ��Ŀ¼��������Ҫ�洢�����ļ��ĵط�������target_dir����ָ����zip�鵵��������Ŀǰ�����ں�ʱ�䣬����ʹ��time.strftime()������á���������.zip��չ��������������target_dirĿ¼�С�

time.strftime()������Ҫ����������ij�����ʹ�õ����ֶ��ơ�%Y�ᱻ�����͵�����������%m�ᱻ01��12֮���һ��ʮ�����·�������������������ơ���Щ���Ƶ���ϸ��������ڡ�Python�ο��ֲᡷ�л�á���Python�ο��ֲᡷ���������Python���а��С�ע����Щ����������print���Ķ��ƣ�%���һ��Ԫ�飩���ƣ�������ȫ��ͬ��

����ʹ�üӷ��������� ���� �ַ��������������ַ���������һ�𷵻�һ���µ��ַ�����ͨ�����ַ�ʽ�����Ǵ�����Ŀ��zip�ļ������ơ��������Ǵ�����zip_command�ַ��������������ǽ�Ҫִ�е�����������shell��Linux�ն˻���DOS��ʾ���������������Լ������Ƿ�����

zip������һЩѡ��Ͳ�����-qѡ��������ʾzip����������������-rѡ���ʾzip�����Ŀ¼�ݹ������������������Ŀ¼�Լ���Ŀ¼�е��ļ�������ѡ�������ϳ���д��ʽ-qr��ѡ�������Ŵ�������zip�鵵�����ƣ�Ȼ�����Ǵ����ݵ��ļ���Ŀ¼�б�������ʹ���Ѿ�ѧϰ�����ַ���join������source�б�ת��Ϊ�ַ�����

�������ʹ��os.system���� ���� ���������������ͺ����� ϵͳ ����������һ��������shell������������������ɹ����У�������0�����������ش���š�

�����������������Ǵ�ӡ��Ӧ����Ϣ����ʾ�����Ƿ񴴽��ɹ������ˣ��������������Ѿ�������һ���ű��������ǵ���Ҫ�ļ������ݣ�

��Windows�û���ע��
����԰�source�б���targetĿ¼���ó��κ��ļ���Ŀ¼����������Windows�����С��һЩ��������Windows�ѷ�б�ܣ�\����ΪĿ¼�ָ�������Python�÷�б�ܱ�ʾת�����
���ԣ����ʹ��ת�������ʾ��б�ܱ�������ʹ����Ȼ�ַ��������磬ʹ��'C:\\Documents'��r'C:\Documents'������'C:\Documents'��������ʹ��һ����֪����ת���\D��

���������Ѿ�����һ�����Թ����ı��ݽű������ǿ������κ�������Ҫ�����ļ����ݵ�ʱ��ʹ����������Linux/Unix�û�ʹ��ǰ����ܵ���ִ�еķ����������Ϳ������κεط��κ�ʱ�����б��ݽű��ˡ��ⱻ��Ϊ������ʵʩ���ڻ��������ڡ�

����ij��������ȷ���������ǣ�ͨ������һ�����򲢲�����������������ȫһ�������磬������Щ������û�����ǡ�����ֻ���������������ʱ������һ����󣬵ȵȡ���������£���Ӧ�ûص���ƻ��ڻ��ߵ��Գ���

�汾��

��һ���汾�Ľű����Թ�����Ȼ�������ǿ��Զ�����Щ�Ż��Ա����������ǵ��ճ������б�ø��á����Ϊ������ά�����ڡ�

����Ϊ�Ż�֮һ�Dz��ø��õ��ļ������ơ���ʹ�� ʱ�� ��Ϊ�ļ���������ǰ�� ���� ��ΪĿ¼���������������Ŀ¼�С���������һ����������ı��ݻ��Եȼ��ṹ�洢��������͸������׹����ˡ�����һ���������ļ����ij���Ҳ���Ա�̡�����һ�������Dz��ø��Զ������ļ��п��԰����㷽��ؼ������Ƿ���ÿһ�촴���˱��ݣ���Ϊֻ�����㴴���˱��ݣ��Ż���������Ŀ¼��

��10.2 ���ݽű������汾��

#!/usr/bin/python
# Filename: backup_ver2.py


import os
import time

# 1. The files and directories to be backed up are specified in a list.
source = ['/home/swaroop/byte', '/home/swaroop/bin']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that

# 2. The backup must be stored in a main backup directory

target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using

# 3. The files are backed up into a zip file.
# 4. The current day is the name of the subdirectory in the main directory

today = target_dir + time.strftime('%Y%m%d')
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')

# Create the subdirectory if it isn't already there
if not os.path.exists(today):
    os.mkdir(today) # make directory
    print 'Successfully created directory', today

# The name of the zip file
target = today + os.sep + now + '.zip'

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

# Run the backup
if os.system(zip_command) == 0:
    print 'Successful backup to', target
else:
    print 'Backup FAILED'

��Դ�ļ���code/backup_ver2.py��

���

$ python backup_ver2.py
Successfully created directory /mnt/e/backup/20041208
Successful backup to /mnt/e/backup/20041208/080020.zip

$ python backup_ver2.py
Successful backup to /mnt/e/backup/20041208/080428.zip

������

��������Ĵ󲿷�����ͬ�ġ��ı�IJ�����Ҫ��ʹ��os.exists����������������Ŀ¼���Ƿ����Ե�ǰ������Ϊ���Ƶ�Ŀ¼�����û�У�����ʹ��os.mkdir����������

ע��os.sep�������÷�������������IJ���ϵͳ����Ŀ¼�ָ���������Linux��Unix������'/'����Windows������'\\'������Mac OS������':'��ʹ��os.sep����ֱ��ʹ���ַ�����ʹ���ǵij��������ֲ�ԣ�������������Щϵͳ�¹�����

�汾��

�ڶ����汾�������϶౸�ݵ�ʱ�򻹹����ò�������������м��౸�ݵ�ʱ���ҷ���Ҫ����ÿ�������Ǹ�ʲô�ģ�����ʮ�����ѣ����磬�ҿ��ܶԳ�������ݽ�������һЩ��Ҫ�ĸı䣬��������Ҫ����Щ�ı���zip�鵵��������ϵ�����������ͨ����zip�鵵���ϸ���һ���û��ṩ��ע���������ʵ�֡�

��10.3 ���ݽű������汾��������������

#!/usr/bin/python
# Filename: backup_ver3.py


import os
import time

# 1. The files and directories to be backed up are specified in a list.
source = ['/home/swaroop/byte', '/home/swaroop/bin']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that

# 2. The backup must be stored in a main backup directory

target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using

# 3. The files are backed up into a zip file.
# 4. The current day is the name of the subdirectory in the main directory

today = target_dir + time.strftime('%Y%m%d')
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')

# Take a comment from the user to create the name of the zip file
comment = raw_input('Enter a comment --> ')
if len(comment) == 0: # check if a comment was entered
    target = today + os.sep + now + '.zip'
else:
    target = today + os.sep + now + '_' +
        comment.replace(' ', '_') + '.zip'

# Create the subdirectory if it isn't already there
if not os.path.exists(today):
    os.mkdir(today) # make directory
    print 'Successfully created directory', today

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

# Run the backup
if os.system(zip_command) == 0:
    print 'Successful backup to', target
else:
    print 'Backup FAILED'

��Դ�ļ���code/backup_ver3.py��

���

$ python backup_ver3.py
File "backup_ver3.py", line 25
target = today + os.sep + now + '_' +
                                ^
SyntaxError: invalid syntax

�������������

������򲻹�����Python˵��һ���﷨��������ζ�Žű�������Python����ʶ��Ľṹ�������ǹ۲�Python�����Ĵ����ʱ����Ҳ���������������������λ�á��������Ǵ����п�ʼ ���� ���ǵij���

ͨ����ϸ�Ĺ۲죬���Ƿ���һ���߼��б��ֳ������������У��������Dz�û��ָ������������������ͬһ�߼��С������ϣ�Python���ּӷ�����������������һ�߼���û���κβ��������������֪������μ�������ס���ǿ���ʹ��������β�ķ�б������ʾ�߼�������һ�����м��������ԣ����������˳����ⱻ��Ϊ�޶���

�汾��

��10.4 ���ݽű������汾��

#!/usr/bin/python
# Filename: backup_ver4.py


import os
import time

# 1. The files and directories to be backed up are specified in a list.
source = ['/home/swaroop/byte', '/home/swaroop/bin']
# If you are using Windows, use source = [r'C:\Documents', r'D:\Work'] or something like that

# 2. The backup must be stored in a main backup directory

target_dir = '/mnt/e/backup/' # Remember to change this to what you will be using

# 3. The files are backed up into a zip file.
# 4. The current day is the name of the subdirectory in the main directory

today = target_dir + time.strftime('%Y%m%d')
# The current time is the name of the zip archive
now = time.strftime('%H%M%S')

# Take a comment from the user to create the name of the zip file
comment = raw_input('Enter a comment --> ')
if len(comment) == 0: # check if a comment was entered
    target = today + os.sep + now + '.zip'
else:
    target = today + os.sep + now + '_' + \
        comment.replace(' ', '_') + '.zip'
    # Notice the backslash!

# Create the subdirectory if it isn't already there
if not os.path.exists(today):
    os.mkdir(today) # make directory
    print 'Successfully created directory', today

# 5. We use the zip command (in Unix/Linux) to put the files in a zip archive
zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))

# Run the backup
if os.system(zip_command) == 0:
    print 'Successful backup to', target
else:
    print 'Backup FAILED'

��Դ�ļ���code/backup_ver4.py��

���

$ python backup_ver4.py
Enter a comment --> added new examples
Successful backup to /mnt/e/backup/20041208/082156_added_new_examples.zip

$ python backup_ver4.py
Enter a comment -->
Successful backup to /mnt/e/backup/20041208/082316.zip

������

����������ڹ����ˣ������ǿ�һ�°汾����������ʵ���ԸĽ�������ʹ��raw_input�����õ��û���ע�ͣ�Ȼ��ͨ��len�����ҳ�����ij����Լ����û��Ƿ�ȷʵ������ʲô����������û�ֻ�ǰ����س���������ֻ��һ���������ݣ�û����ʲô�ر���޸ģ�����ô���Ǿ���֮ǰ��������������

Ȼ��������ṩ��ע�ͣ���ô���ᱻ���ӵ�zip�鵵��������.zip��չ��֮ǰ��ע�����ǰ�ע���еĿո��滻���»��ߡ���������Ϊ�����������ļ���Ҫ���׵öࡣ

��һ���Ż�

���ڴ�����û���˵�����ĸ��汾��һ������Ĺ����ű��ˣ���������Ȼ�н�һ���Ľ��Ŀռ䡣���磬������ڳ����а��� ���� �̶ȡ����������-vѡ����ʹ��ij�����߽����ԡ�

��һ�����ܵĸĽ���ʹ�ļ���Ŀ¼�ܹ�ͨ��������ֱ�Ӵ��ݸ��ű������ǿ���ͨ��sys.argv�б�����ȡ���ǣ�Ȼ�����ǿ���ʹ��list���ṩ��extend���������Ǽӵ�source�б���ȥ��

�һ�ϣ���е�һ���Ż���ʹ��tar�������zip�����������һ��������������ʹ��tar��gzip�����ʱ�򣬱��ݻ�����С���������Ҫ��Windows��ʹ����Щ�鵵��WinZipҲ�ܷ���ش�����Щ.tar.gz�ļ���tar�����ڴ����Linux/Unixϵͳ�ж���Ĭ�Ͽ��õġ�Windows�û�Ҳ����������װ����

�����ַ������ڽ���Ϊ��

tar = 'tar -cvzf %s %s -X /home/swaroop/excludes.txt' % (target, ' '.join(srcdir))

ѡ��������£�

��Ҫ
������Ĵ�����Щ�鵵�ķ����Ƿֱ�ʹ��zipfile��tarfile��������Python��׼���һ���֣����Թ���ʹ�á�ʹ����Щ��ͱ�����ʹ��os.system������Ƽ�ʹ�õĺ������������������صĴ���
Ȼ�������ڱ�����ʹ��os.system�ķ������������ݣ��ⴿ����Ϊ�˽�ѧ����Ҫ�������Ļ������ӾͿ��Լ򵥵���ÿ���˶��ܹ����⣬ͬʱҲ�Ѿ��㹻���ˡ�


��һҳ ��һ�� ��һҳ
���� ��ҳ ������������