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
*
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!!!'