python之模块 os
# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#python之模块 os import os
'''
FUNCTIONS
abort(...)#暂不了解
abort() -> does not return! Abort the interpreter immediately. This 'dumps core' or otherwise fails
in the hardest way possible on the hosting operating system. access(...)#暂不了解
access(path, mode) -> True if granted, False otherwise Use the real uid/gid to test for access to a path. Note that most
operations will use the effective uid/gid, therefore this routine can
be used in a suid/sgid environment to test if the invoking user has the
specified access to the path. The mode argument can be F_OK to test
existence, or the inclusive-OR of R_OK, W_OK, and X_OK. chdir(...)#改变当前工作目录,改变工作目录到dirname,相当于shell下cd
chdir(path) Change the current working directory to the specified path. chmod(...)
chmod(path, mode) Change the access permissions of a file. close(...)
close(fd) Close a file descriptor (for low level IO). closerange(...)
closerange(fd_low, fd_high) Closes all file descriptors in [fd_low, fd_high), ignoring errors. dup(...)
dup(fd) -> fd2 Return a duplicate of a file descriptor. dup2(...)
dup2(old_fd, new_fd) Duplicate file descriptor. execl(file, *args)
execl(file, *args) Execute the executable file with argument list args, replacing the
current process. execle(file, *args)
execle(file, *args, env) Execute the executable file with argument list args and
environment env, replacing the current process. execlp(file, *args)
execlp(file, *args) Execute the executable file (which is searched for along $PATH)
with argument list args, replacing the current process. execlpe(file, *args)
execlpe(file, *args, env) Execute the executable file (which is searched for along $PATH)
with argument list args and environment env, replacing the current
process. execv(...)
execv(path, args) Execute an executable path with arguments, replacing current process. path: path of executable file
args: tuple or list of strings execve(...)
execve(path, args, env) Execute a path with arguments and environment, replacing current process. path: path of executable file
args: tuple or list of arguments
env: dictionary of strings mapping to strings execvp(file, args)
execvp(file, args) Execute the executable file (which is searched for along $PATH)
with argument list args, replacing the current process.
args may be a list or tuple of strings. execvpe(file, args, env)
execvpe(file, args, env) Execute the executable file (which is searched for along $PATH)
with argument list args and environment env , replacing the
current process.
args may be a list or tuple of strings. fdopen(...)
fdopen(fd [, mode='r' [, bufsize]]) -> file_object Return an open file object connected to a file descriptor. fstat(...)
fstat(fd) -> stat result Like stat(), but for an open file descriptor. fsync(...)
fsync(fildes) force write of file with filedescriptor to disk. getcwd(...)#获取当前目录
getcwd() -> path Return a string representing the current working directory. getcwdu(...)
getcwdu() -> path Return a unicode string representing the current working directory. getenv(key, default=None)
Get an environment variable, return None if it doesn't exist.
The optional second argument can specify an alternate default. getpid(...)
getpid() -> pid Return the current process id isatty(...)
isatty(fd) -> bool Return True if the file descriptor 'fd' is an open file descriptor
connected to the slave end of a terminal. kill(...)
kill(pid, sig) Kill a process with a signal. listdir(...)#获取目录内容,其结果为list类型
listdir(path) -> list_of_strings Return a list containing the names of the entries in the directory. path: path of directory to list The list is in arbitrary order. It does not include the special
entries '.' and '..' even if they are present in the directory. lseek(...)
lseek(fd, pos, how) -> newpos Set the current position of a file descriptor.
Return the new cursor position in bytes, starting from the beginning. lstat(...)
lstat(path) -> stat result Like stat(path), but do not follow symbolic links. makedirs(name, mode=511)#递归文件夹创建函数,
#os.makedirs('dirname1/dirname2') 可生成多层递归目录
makedirs(path [, mode=0777]) Super-mkdir; create a leaf directory and all intermediate ones.
Works like mkdir, except that any intermediate path segment (not
just the rightmost) will be created if it does not exist. This is
recursive. mkdir(...)#创建一个新的目录,目录已存在会报错,WindowsError: [Error 183] : '1'
mkdir(path [, mode=0777]) Create a directory. open(...)
open(filename, flag [, mode=0777]) -> fd Open a file (for low level IO). pipe(...)
pipe() -> (read_end, write_end) Create a pipe. popen(...)
popen(command [, mode='r' [, bufsize]]) -> pipe Open a pipe to/from a command returning a file object. popen2(...) popen3(...) popen4(...) putenv(...)
putenv(key, value) Change or add an environment variable. read(...)
read(fd, buffersize) -> string Read a file descriptor. remove(...)#删除文件,参数可根绝对路径
remove(path) Remove a file (same as unlink(path)). removedirs(name)#递归删除目录
在os模块中使用removedirs方法时,要想把a目录和a目录下的b目录同时删除,代码os.removedirs(r'D:\a\b'),只有符合以下条件时,a和b两个目录才会被同时删除。
1)a目录下只有b目录
2)b目录中必须是一个空目录
两者缺一不可
removedirs(path) Super-rmdir; remove a leaf directory and all empty intermediate
ones. Works like rmdir except that, if the leaf directory is
successfully removed, directories corresponding to rightmost path
segments will be pruned away until either the whole path is
consumed or an error occurs. Errors during this latter phase are
ignored -- they generally mean that a directory was not empty. rename(...)#文件重命名
rename(old, new) Rename a file or directory. renames(old, new)#递归重命名文件夹或者文件,暂不知道怎么使用
renames(old, new) Super-rename; create directories as necessary and delete any left
empty. Works like rename, except creation of any intermediate
directories needed to make the new pathname good is attempted
first. After the rename, directories corresponding to rightmost
path segments of the old name will be pruned until either the
whole path is consumed or a nonempty directory is found. Note: this function can fail with the new directory structure made
if you lack permissions needed to unlink the leaf directory or
file. rmdir(...)#删除空目录,如果目录非空报错,WindowsError: [Error 145] : '1'
rmdir(path) Remove a directory. spawnl(mode, file, *args)
spawnl(mode, file, *args) -> integer Execute file with arguments from args in a subprocess.
If mode == P_NOWAIT return the pid of the process.
If mode == P_WAIT return the process's exit code if it exits normally;
otherwise return -SIG, where SIG is the signal that killed it. spawnle(mode, file, *args)
spawnle(mode, file, *args, env) -> integer Execute file with arguments from args in a subprocess with the
supplied environment.
If mode == P_NOWAIT return the pid of the process.
If mode == P_WAIT return the process's exit code if it exits normally;
otherwise return -SIG, where SIG is the signal that killed it. spawnv(...)
spawnv(mode, path, args) Execute the program 'path' in a new process. mode: mode of process creation
path: path of executable file
args: tuple or list of strings spawnve(...)
spawnve(mode, path, args, env) Execute the program 'path' in a new process. mode: mode of process creation
path: path of executable file
args: tuple or list of arguments
env: dictionary of strings mapping to strings startfile(...)
startfile(filepath [, operation]) - Start a file with its associated
application. When "operation" is not specified or "open", this acts like
double-clicking the file in Explorer, or giving the file name as an
argument to the DOS "start" command: the file is opened with whatever
application (if any) its extension is associated.
When another "operation" is given, it specifies what should be done with
the file. A typical operation is "print". startfile returns as soon as the associated application is launched.
There is no option to wait for the application to close, and no way
to retrieve the application's exit status. The filepath is relative to the current directory. If you want to use
an absolute path, make sure the first character is not a slash ("/");
the underlying Win32 ShellExecute function doesn't work if it is. stat(...)
#os.stat('path/filename') 获取文件/目录信息
stat(path) -> stat result Perform a stat system call on the given path. stat_float_times(...)
stat_float_times([newval]) -> oldval Determine whether os.[lf]stat represents time stamps as float objects.
If newval is True, future calls to stat() return floats, if it is False,
future calls return ints.
If newval is omitted, return the current setting. strerror(...)
strerror(code) -> string Translate an error code to a message string. system(...)#运行shell命令
#os.system('cmd') #启动dos
#启动cmd命令符
Execute the command (a string) in a subshell. tempnam(...)
tempnam([dir[, prefix]]) -> string Return a unique name for a temporary file.
The directory and a prefix may be specified as strings; they may be omitted
or None if not needed. times(...)
times() -> (utime, stime, cutime, cstime, elapsed_time) Return a tuple of floating point numbers indicating process times. tmpfile(...)
tmpfile() -> file object Create a temporary file with no directory entries. tmpnam(...)
tmpnam() -> string Return a unique name for a temporary file. umask(...)
umask(new_mask) -> old_mask Set the current numeric umask and return the previous umask. unlink(...)
unlink(path) Remove a file (same as remove(path)). urandom(...)
urandom(n) -> str Return n random bytes suitable for cryptographic use. utime(...)
utime(path, (atime, mtime))
utime(path, None) Set the access and modified time of the file to the given values. If the
second form is used, set the access and modified times to the current time. waitpid(...)
waitpid(pid, options) -> (pid, status << 8) Wait for completion of a given process. options is ignored on Windows. walk(top, topdown=True, onerror=None, followlinks=False)#目录遍历
Directory tree generator. For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), yields a 3-tuple
WindowsError: [Error 183] : '1'
dirpath, dirnames, filenames Example: import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
print root, "consumes",
print sum([getsize(join(root, name)) for name in files]),
print "bytes in", len(files), "non-directory files"
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories write(...)
write(fd, string) -> byteswritten Write a string to a file descriptor. DATA:
os.name#输出字符串指示正在使用的平台。如果是window 则用'nt'表示,对于Linux/Unix用户,它是'posix
os.linesep字符串给出当前平台使用的行终止符 '\r\n' #Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'。
os.curdir 返回当前目录: ('.')
os.pardir 获取当前目录的父目录字符串名:('..')
os.environ 获取系统环境变量 >>>
'''
python之模块 os的更多相关文章
- Python文件属性模块Os.path
Python文件属性模块Os.path介绍 os.path模块主要用于文件属性获取和判断,在编程中会经常用到,需要熟练掌握.以下是该模块的几种常用方法. os.path官方文档:http://docs ...
- python之模块(os、sys、json、subprocess)
目录 os模块 sys模块 json模块 subprocess模块 os模块 os模块主要是与操作系统打交道. 导入os模块 import os 创建单层文件夹,路径必须要存在 os.mkdir(路径 ...
- Python标准模块--os
1.模块简介 os模块主要包含普遍的操作系统相关操作,如果开发者希望自己开发的Python应用能够与平台无关,尤其需要关注os这个模块. 2.模块使用 2.1 os模块 1. os.name,输出字符 ...
- Python默认模块 os和shutil 实用函数
os.sep 可以取代操作系统特定的路径分隔符.windows下为 '\\' os.name 字符串指示你正在使用的平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是 ' ...
- python常用模块os和sys
一.os模块 说明:os模块是对操作系统进行调用的接口 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 os. ...
- Python常用模块os & sys & shutil模块
OS模块 import os ''' os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录: ...
- python 操作系统模块 -- OS
os,语义为操作系统,模块提供了访问多个操作系统服务的功能,可以处理文件和目录这些我们日常手动需要做的操作.os和它的子模块os.path还包括一些用于检查.构造.删除目录和文件的函数,以及一些处理路 ...
- python --- 23 模块 os sys pickle json
一. os模块 主要是针对操作系统的 用于文件操作 二. sys 模块 模块的查找路径 sys.path 三.pickle 模块 1. pickle.dumps(对象) 序列化 把对 ...
- python - 常用模块 os, sys
常用模块: os(处理文件和目录), sys(sys 模块包含了与 Python 解释器和它的环境有关的函数.) sys.argv 变量是一个字符串的 列表.特别地,sys.argv 包含了 命令行参 ...
随机推荐
- Unexpected identifier in composer-common/lib/cardstore/businessnetworkcardstore.js:54
c错误描述 Unexpected identifier in composer-common/lib/cardstore/businessnetworkcardstore.js:54 yo hyper ...
- [转]MCC(移动国家码)和 MNC(移动网络码)
From : http://blog.chinaunix.net/uid-20484604-id-1941290.html 国际移动用户识别码(IMSI) international mobi ...
- ENC28J60学习笔记——第1部分
1前言 嵌入式以太网开发,可以分为两个部分,一个是以太网收发芯片的使用,一个是嵌入式以太网协议栈的实现.以太网收发芯片的使用要比串口收发芯片的使用复杂的多,市面上流通比较广泛的以太网收发芯片种类还不少 ...
- libcurl HTTP POST请求向服务器发送json数据【转】
转载:http://blog.csdn.net/dgyanyong/article/details/14166217 转载:http://blog.csdn.net/th_gsb/article/de ...
- Spring Boot 直接用jar运行项目
概述 在Spring Boot 开篇-创建和运行一文中,介绍了如何创建一个Sprint Boot项目并且运行起来.但是运行的方式是在IDEA中直接Run起来的.还有另一中方式可以可以把Spring B ...
- xgboost入门与实战(实战调参篇)
https://blog.csdn.net/sb19931201/article/details/52577592 xgboost入门与实战(实战调参篇) 前言 前面几篇博文都在学习原理知识,是时候上 ...
- capwap学习笔记——capwap的前世今生(转)
公司要做AP和AC,从今天开始学习capwap. 1 capwap的前世今生 1.1 胖AP.瘦AP.AC 传统的WLAN网络都是为企业或家庭内少量移动用户的接入而组建的.因此,只需要一个无线路由器就 ...
- sql server2008R2 无法连接到WMI提供程序。你没有权限或者该服务器无法访问
在自己的Win8.1的系统在安装了Vs2013和Sqlserver2008R2 今天在打开ssms的时候发现连接不上数据库,且出现了以下问题 然后打开Sqlserver配置管理器准备看看sqlserv ...
- 闲聊DNN CTR预估模型
原文:http://www.52cs.org/?p=1046 闲聊DNN CTR预估模型 Written by b manongb 作者:Kintocai, 北京大学硕士, 现就职于腾讯. 伦敦大学张 ...
- mysql CAPI 接口 读取中文乱码的解决方案(转)
最近的yymysqlsdk的开源项目里,对中文的支持不到位,因此用了1.5天的时间,对中文处理的各个情况进行了分析. 1.首先确认你的MySQL配置文件,my.ini (只针对window ...