So cute are you python 5

本文将通过Python脚本详细介绍了如何使用对象、字符串操作和备份功能。包括简单对象赋值、对象复制、字符串判断与连接等基本概念和实践。此外,还展示了如何创建备份文件,确保数据安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Learn python step by step

1.show how to using an objcet

#!/usr/bin/python
#coding:utf-8
#FileName: referance.py
# function: show how to using an objcet
print 'Simple Assignment'
shoplist = ['apple','mango','carrot','banana']
mylist = shoplist # same object
del shoplist[ 0]
print 'shoplist is',shoplist
print 'mylist is',mylist
print 'Copy by making a full slice'
mylist =shoplist[:]
del mylist[ 0]
print 'shoplist is',shoplist
print 'mylist is',mylist
*
2.How to using string with python
#!/usr/bin/python
#coding:utf-8
#FileName: str_methods.py
# function:How to using string with python
name = 'Swaroop'#This is a string object
if name.startswith('Swa'):
    print 'Yes,the string starta with "Swa"'
if 'a' in name:
    print 'Yes,it contains the string "a"'
if name.find('war')!=- 1:
    print 'Yes,it contains the string "war"'
delimiter = '_*_'
mylist = ['Brazil','Russia','India','China']
print delimiter.join(mylist)
*

3.To show an backup function

#!/usr/bin/python
#coding:utf-8
#FileName: backup_ver1.py
# function:To show an backup function
import os
import time
#1.The files and directories to be backed up are specified in a list
source = ['~/work/python/b/reference.py','~/work/python/b/str_methods.py']
#If you using windows ,use source =['D:/work/cc','C:/wrk/dd']
#2.The backup must be stored in a main backup directory
target_dir ='~/work/python/b/backup/'
#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 linux/unix) to backup
zip_command = "zip -qr '%s' %s " %(target,' '.join(source))
#Run backup
if os.system(zip_command)== 0:
    print 'Successful backup to',target
else:
    print 'Backup FAILED'
*
4.To show an backup function again
#!/usr/bin/python
#coding:utf-8
#FileName: backup_ver2.py
# function:To show an backup function
import os
import time
#1.The files and directories to be backed up are specified in a list
source = ['~/work/python/b/reference.py','~/work/python/b/str_methods.py']
#If you using windows ,use source =['D:/work/cc','C:/wrk/dd']
#2.The backup must be stored in a main backup directory
target_dir ='~/work/python/b/backup/'
#3.The files are backed up into a zip file.
today =target_dir+time.strftime('%Y%m%d')
#4.The name of the zip archive is the current date and time
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
tartget =today+os.sep+now+'.zip'
#5.We use the zip command to zip files
zip_command ="zip -qr '%s' %s" %(target,' '.join(source))
#Run the backup
if os.system(zip_command)== 0:
    print 'Succssful backup to',target
else:
    print 'Backup FAILED!!!'


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值