The os and sys python

2011-05-09  来源:本站原创  分类:Python  人气:87 

http://linux.chinaitlab.com/manual/python_chinese/ch14.html

backup ------------

#!/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'

------------------
os.name string indicates that you are using the platform. Such as for Windows, it is the 'nt', and for Linux / Unix users, it is 'posix'.

os.getcwd () function gets the current working directory that the current working directory path Python script.

os.getenv () and os.putenv () functions are used to read and set environment variables.

os.listdir () returns the specified directory of all files and directories.

os.remove () function is used to delete a file.

os.system () function is used to run shell commands.

os.linesep given the current platform string line terminator. For example, Windows uses '\ r \ n', Linux uses '\ n' and the Mac using '\ r'.

os.path.split () function returns a path to the directory name and file name.

>>> Os.path.split ('/ home / swaroop / byte / code / poem.txt')
('/ Home / swaroop / byte / code', 'poem.txt')

os.path.isfile () and os.path.isdir () function check if the given path is a file or directory. Similarly, os.path.existe () function is used to check if the given path actually exists.

相关文章
  • The os and sys python 2011-05-09

    http://linux.chinaitlab.com/manual/python_chinese/ch14.html backup ------------ #!/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',

  • Mac os X lion Python MysqlDB 2012-04-23

    编译方法:下载最新Mysql-Python-1.2.3 Mysql 5.5.21 64位 Mac os X lion 64位 python 2.7 64位 系统默认不安装gcc,所以需要去Xcode安装gcc command line 最后sudo python setup.py install 就可以了 python import MySQLdb 成功

  • Python 命令行参数sys.argv 2013-12-22

    命令行参数是通过sys.argv[]来获取的,sys.argv[0]是代码文件本身的路径,因此参数是从1开始的.比如设置参数为: spe Python代码: import os, sys os.system(sys.argv[1]) 带参数执行 python xxx.py spe os.system 是用来执行命令行的.因此该程序会接收到第一个参数spe,然后在命令行里执行spe,这样,spe(Python IDE)就打开了.

  • python os that experience 2010-10-10

    A few days ago made a file to replace the function to use some python os functions, sensory python os module's function is very powerful, If you want your python program to work with platform-independent, then this module is particularly important. T

  • Flow arrangements for software development (as an example in Python language) 2009-05-31

    Python language in the following example to illustrate the flow of software development arrangements For software development, implementation function of the preparation of the development of software code is only a small part of the whole story but

  • python database applications 2010-03-19

    More recent use of the database is sqlite, the machine itself I upload the activepython, comes with sqlite3, I write the code to store data as follows: def store_data(id_name,sq_data): """ Gets the data comparison and inserted into the sqli

  • Using Python to delete non-empty directories and files 2010-05-11

    Project resulting in some temporary files and compile CVS directories, source code for each package must be manually deleted a lot of trouble. About using Python to simplify this process! Had wanted to use the bat file to deal with Windows, but did n

  • python multithreading ctrl-c out of the question 2010-05-24

    Scene: Often encounter the following issues: Many io busy for usage, multi-threaded approach to Jiejue, but this time to find python Command OK not respond to ctrl-c Le, and the corresponding java code Ze no problem: public class Test { public static

  • linux python coding questions and other related miscellaneous 2010-07-18

    Causes: Lang set linux system regardless of what the file name must encode into utf-8 of str objects, os.path.exists to proper use. (When the locale is zh_CN.utf-8 When , u "path" approach can also be, may be turning themselves inside) Also the

  • Using python script (rich shell function) 2010-11-05

    os.system (command) os.system ("ls-l"), you can see the "ls" lists the files in the current directory commonly used commands in the python shell usually has a corresponding written. ls python: os.listdir (dirname) cp shutil.copy (src,

  • [Turn] a newcomer how to learn python, how to learn python? 2010-12-01

    http://www.hackbase.com/subject/2009-12-30/17120.html According to my learning experience, I concluded the following ten to share: 1) The first step to learn python is the right to www.python.org website to download a python version. I recommend that

  • The temporary files generated python 2010-12-14

    #!/usr/bin/evn python import os; import sys; import uuid; def WriteStrToFile(strFile, strVal): bRet = False; file = None; try: file = open(strFile, "wb"); file.write(strVal); bRet = True; finally: if file: file.close(); return bRet; if "__m

  • A basic knowledge of Python 3.1 2010-12-30

    base.py #!usr/bin/python # -*- coding: utf-8 -*- # Filename : base.py # Author : amos_tl # Date : 2010-12-30 ''' PYTHON Basics Linux When users need to give permission to perform : chmod a+x base.py ''' # 1. Tuples Tuple : Simple understanding is sep

  • 使用python 获取进程pid号的方法 2013-11-04

    这篇文章主要介绍了使用python 获取进程pid号的方法,需要的朋友可以参考下 保存为.py文件后 运行脚本在后面添加进程名称即可 比如:python proinfo.py qq 即可获取QQ的进程信息,注意不区分大小写 #-*- encoding:UTF-8 -*- import os import sys import string import psutil import re def get_pid(name): process_list = psutil.get_process_li

  • 可用于监控 mysql Master Slave 状态的python代码 2013-11-15

    用于监控MySQL Master Slave 状态的python代码,有需要的朋友可以参考下 import os import sys import MySQLdb def getStatus(conn): query = " SHOW SLAVE STATUS " # print query cursor = conn.cursor() cursor.execute(query) result = cursor.fetchall() return result[0] def reso

  • Windows下用py2exe将Python程序打包成exe程序的教程 2013-11-16

    这篇文章主要介绍了Windows下用py2exe将Python程序打包成exe程序的教程,文中主要针对Python3.x版本进行说明,需要的朋友可以参考下 py2exe在sourceforge 的下载只支持到2.7. 针对python3.0+的版本,需要自己编译. 1.下载源码 svn checkout svn://svn.code.sf.net/p/py2exe/svn/trunk py2exe-svn 2.编译环境 这里使用的是vs2014. 3.安装 进入py2exe-3 python s

  • Python写的服务监控程序实例 2013-11-25

    这篇文章主要介绍了Python写的服务监控程序实例,本文直接给出实现代码,需要的朋友可以参考下 前言: Redhat下安装Python2.7 rhel6.4自带的是2.6, 发现有的机器是python2.4. 到python网站下载源代码,解压到Redhat上,然后运行下面的命令: # ./configure --prefix=/usr/local/python27 # make # make install 这样安装之后默认不会启用Python2.7,需要使用/usr/local/python

  • python 从远程服务器下载日志文件的程序 2013-11-29

    本文提供了一种方法,该程序可实现抓取服务器的日志文件到本地,根据该程序可自行扩展对日志文件的自动化分析.常用于数据挖掘.生产维护等 import os import sys import ftplib import socket ################################################################## # sign in the ftp server and download the log file. # 登陆生产服务器下载日志 ##

  • 使用Python获取Linux系统的各种信息 2013-12-05

    这篇文章主要介绍了使用Python获取Linux系统的各种信息,例如系统类型.CPU信息.内存信息.块设备等,需要的朋友可以参考下 在本文中,我们将会探索使用Python编程语言工具来检索Linux系统各种信息.走你. 哪个Python版本? 当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相同的代码在CPython 3 (3.3)上是不工作的,以及提供一份解释不同之处的备选代码.请确保你已经安装了CPython,在终端上输入python或者python3回

  • Python实现的下载8000首儿歌的代码分享 2013-12-05

    这篇文章主要介绍了Python实现的下载8000首儿歌的代码分享,本文直接给出实现代码,下载的是有伴网的资源,需要的朋友可以参考下 下载8000首儿歌的python的代码: #-*- coding: UTF-8 -*- from pyquery import PyQuery as py from lxml import etree import urllib import re import os import sys import logging def format(filename): t