您的位置:首页 > 运维架构 > Linux

windows和linux下的python备份脚本

2010-07-20 22:20 477 查看
说两点:

1 windows下如果不指定编码格式他会报乱码错误

Non-ASCII character '/xd6' in file c:/2.py on line 5 说是存在非ASCII字符在文件中,通过网址http://www.python.org/peps/pep-0263.html 查找说

Without interpreter line, using plain text:

# This Python file uses the following encoding: utf-8
import os, sys

加上这种declare的话windows下就可以任意输入汉字而不报错了,注意如果路径包含空格记得用""括起来就可以了

2 ' '.join() 代表把list转换为字符串

WINDOWS:

# encoding: utf-8
import os
import time
import sys
source = [r'"E:/我 靠"']
target_dir = r'C:/TDDownload'
target = target_dir  + '//'+ time.strftime('%Y%m%d%H%M%S') + '.zip'
zip_command = "zip -qr %s %s" % (target,' '.join(source))
if os.system(zip_command) == 0:
print 'sucessful backup',target
else:
print 'backup failure'


LINUX:

#!/usr/bin/python
# -*- coding: uft-8 -*-
# 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/无聊', '/home/swaroop/有趣']
# 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'
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: