1. # -*- coding: utf-8 -*-
  2. #python 27
  3. #xiaodeng
  4. #python之模块 os
  5.  
  6. import os
  7. '''
  8. FUNCTIONS
  9. abort(...)#暂不了解
  10. abort() -> does not return!
  11.  
  12. Abort the interpreter immediately. This 'dumps core' or otherwise fails
  13. in the hardest way possible on the hosting operating system.
  14.  
  15. access(...)#暂不了解
  16. access(path, mode) -> True if granted, False otherwise
  17.  
  18. Use the real uid/gid to test for access to a path. Note that most
  19. operations will use the effective uid/gid, therefore this routine can
  20. be used in a suid/sgid environment to test if the invoking user has the
  21. specified access to the path. The mode argument can be F_OK to test
  22. existence, or the inclusive-OR of R_OK, W_OK, and X_OK.
  23.  
  24. chdir(...)#改变当前工作目录,改变工作目录到dirname,相当于shell下cd
  25. chdir(path)
  26.  
  27. Change the current working directory to the specified path.
  28.  
  29. chmod(...)
  30. chmod(path, mode)
  31.  
  32. Change the access permissions of a file.
  33.  
  34. close(...)
  35. close(fd)
  36.  
  37. Close a file descriptor (for low level IO).
  38.  
  39. closerange(...)
  40. closerange(fd_low, fd_high)
  41.  
  42. Closes all file descriptors in [fd_low, fd_high), ignoring errors.
  43.  
  44. dup(...)
  45. dup(fd) -> fd2
  46.  
  47. Return a duplicate of a file descriptor.
  48.  
  49. dup2(...)
  50. dup2(old_fd, new_fd)
  51.  
  52. Duplicate file descriptor.
  53.  
  54. execl(file, *args)
  55. execl(file, *args)
  56.  
  57. Execute the executable file with argument list args, replacing the
  58. current process.
  59.  
  60. execle(file, *args)
  61. execle(file, *args, env)
  62.  
  63. Execute the executable file with argument list args and
  64. environment env, replacing the current process.
  65.  
  66. execlp(file, *args)
  67. execlp(file, *args)
  68.  
  69. Execute the executable file (which is searched for along $PATH)
  70. with argument list args, replacing the current process.
  71.  
  72. execlpe(file, *args)
  73. execlpe(file, *args, env)
  74.  
  75. Execute the executable file (which is searched for along $PATH)
  76. with argument list args and environment env, replacing the current
  77. process.
  78.  
  79. execv(...)
  80. execv(path, args)
  81.  
  82. Execute an executable path with arguments, replacing current process.
  83.  
  84. path: path of executable file
  85. args: tuple or list of strings
  86.  
  87. execve(...)
  88. execve(path, args, env)
  89.  
  90. Execute a path with arguments and environment, replacing current process.
  91.  
  92. path: path of executable file
  93. args: tuple or list of arguments
  94. env: dictionary of strings mapping to strings
  95.  
  96. execvp(file, args)
  97. execvp(file, args)
  98.  
  99. Execute the executable file (which is searched for along $PATH)
  100. with argument list args, replacing the current process.
  101. args may be a list or tuple of strings.
  102.  
  103. execvpe(file, args, env)
  104. execvpe(file, args, env)
  105.  
  106. Execute the executable file (which is searched for along $PATH)
  107. with argument list args and environment env , replacing the
  108. current process.
  109. args may be a list or tuple of strings.
  110.  
  111. fdopen(...)
  112. fdopen(fd [, mode='r' [, bufsize]]) -> file_object
  113.  
  114. Return an open file object connected to a file descriptor.
  115.  
  116. fstat(...)
  117. fstat(fd) -> stat result
  118.  
  119. Like stat(), but for an open file descriptor.
  120.  
  121. fsync(...)
  122. fsync(fildes)
  123.  
  124. force write of file with filedescriptor to disk.
  125.  
  126. getcwd(...)#获取当前目录
  127. getcwd() -> path
  128.  
  129. Return a string representing the current working directory.
  130.  
  131. getcwdu(...)
  132. getcwdu() -> path
  133.  
  134. Return a unicode string representing the current working directory.
  135.  
  136. getenv(key, default=None)
  137. Get an environment variable, return None if it doesn't exist.
  138. The optional second argument can specify an alternate default.
  139.  
  140. getpid(...)
  141. getpid() -> pid
  142.  
  143. Return the current process id
  144.  
  145. isatty(...)
  146. isatty(fd) -> bool
  147.  
  148. Return True if the file descriptor 'fd' is an open file descriptor
  149. connected to the slave end of a terminal.
  150.  
  151. kill(...)
  152. kill(pid, sig)
  153.  
  154. Kill a process with a signal.
  155.  
  156. listdir(...)#获取目录内容,其结果为list类型
  157. listdir(path) -> list_of_strings
  158.  
  159. Return a list containing the names of the entries in the directory.
  160.  
  161. path: path of directory to list
  162.  
  163. The list is in arbitrary order. It does not include the special
  164. entries '.' and '..' even if they are present in the directory.
  165.  
  166. lseek(...)
  167. lseek(fd, pos, how) -> newpos
  168.  
  169. Set the current position of a file descriptor.
  170. Return the new cursor position in bytes, starting from the beginning.
  171.  
  172. lstat(...)
  173. lstat(path) -> stat result
  174.  
  175. Like stat(path), but do not follow symbolic links.
  176.  
  177. makedirs(name, mode=511)#递归文件夹创建函数,
  178. #os.makedirs('dirname1/dirname2') 可生成多层递归目录
  179. makedirs(path [, mode=0777])
  180.  
  181. Super-mkdir; create a leaf directory and all intermediate ones.
  182. Works like mkdir, except that any intermediate path segment (not
  183. just the rightmost) will be created if it does not exist. This is
  184. recursive.
  185.  
  186. mkdir(...)#创建一个新的目录,目录已存在会报错,WindowsError: [Error 183] : '1'
  187. mkdir(path [, mode=0777])
  188.  
  189. Create a directory.
  190.  
  191. open(...)
  192. open(filename, flag [, mode=0777]) -> fd
  193.  
  194. Open a file (for low level IO).
  195.  
  196. pipe(...)
  197. pipe() -> (read_end, write_end)
  198.  
  199. Create a pipe.
  200.  
  201. popen(...)
  202. popen(command [, mode='r' [, bufsize]]) -> pipe
  203.  
  204. Open a pipe to/from a command returning a file object.
  205.  
  206. popen2(...)
  207.  
  208. popen3(...)
  209.  
  210. popen4(...)
  211.  
  212. putenv(...)
  213. putenv(key, value)
  214.  
  215. Change or add an environment variable.
  216.  
  217. read(...)
  218. read(fd, buffersize) -> string
  219.  
  220. Read a file descriptor.
  221.  
  222. remove(...)#删除文件,参数可根绝对路径
  223. remove(path)
  224.  
  225. Remove a file (same as unlink(path)).
  226.  
  227. removedirs(name)#递归删除目录
  228. os模块中使用removedirs方法时,要想把a目录和a目录下的b目录同时删除,代码os.removedirs(r'D:\a\b'),只有符合以下条件时,ab两个目录才会被同时删除。
  229. 1a目录下只有b目录
  230. 2b目录中必须是一个空目录
  231. 两者缺一不可
  232. removedirs(path)
  233.  
  234. Super-rmdir; remove a leaf directory and all empty intermediate
  235. ones. Works like rmdir except that, if the leaf directory is
  236. successfully removed, directories corresponding to rightmost path
  237. segments will be pruned away until either the whole path is
  238. consumed or an error occurs. Errors during this latter phase are
  239. ignored -- they generally mean that a directory was not empty.
  240.  
  241. rename(...)#文件重命名
  242. rename(old, new)
  243.  
  244. Rename a file or directory.
  245.  
  246. renames(old, new)#递归重命名文件夹或者文件,暂不知道怎么使用
  247. renames(old, new)
  248.  
  249. Super-rename; create directories as necessary and delete any left
  250. empty. Works like rename, except creation of any intermediate
  251. directories needed to make the new pathname good is attempted
  252. first. After the rename, directories corresponding to rightmost
  253. path segments of the old name will be pruned until either the
  254. whole path is consumed or a nonempty directory is found.
  255.  
  256. Note: this function can fail with the new directory structure made
  257. if you lack permissions needed to unlink the leaf directory or
  258. file.
  259.  
  260. rmdir(...)#删除空目录,如果目录非空报错,WindowsError: [Error 145] : '1'
  261. rmdir(path)
  262.  
  263. Remove a directory.
  264.  
  265. spawnl(mode, file, *args)
  266. spawnl(mode, file, *args) -> integer
  267.  
  268. Execute file with arguments from args in a subprocess.
  269. If mode == P_NOWAIT return the pid of the process.
  270. If mode == P_WAIT return the process's exit code if it exits normally;
  271. otherwise return -SIG, where SIG is the signal that killed it.
  272.  
  273. spawnle(mode, file, *args)
  274. spawnle(mode, file, *args, env) -> integer
  275.  
  276. Execute file with arguments from args in a subprocess with the
  277. supplied environment.
  278. If mode == P_NOWAIT return the pid of the process.
  279. If mode == P_WAIT return the process's exit code if it exits normally;
  280. otherwise return -SIG, where SIG is the signal that killed it.
  281.  
  282. spawnv(...)
  283. spawnv(mode, path, args)
  284.  
  285. Execute the program 'path' in a new process.
  286.  
  287. mode: mode of process creation
  288. path: path of executable file
  289. args: tuple or list of strings
  290.  
  291. spawnve(...)
  292. spawnve(mode, path, args, env)
  293.  
  294. Execute the program 'path' in a new process.
  295.  
  296. mode: mode of process creation
  297. path: path of executable file
  298. args: tuple or list of arguments
  299. env: dictionary of strings mapping to strings
  300.  
  301. startfile(...)
  302. startfile(filepath [, operation]) - Start a file with its associated
  303. application.
  304.  
  305. When "operation" is not specified or "open", this acts like
  306. double-clicking the file in Explorer, or giving the file name as an
  307. argument to the DOS "start" command: the file is opened with whatever
  308. application (if any) its extension is associated.
  309. When another "operation" is given, it specifies what should be done with
  310. the file. A typical operation is "print".
  311.  
  312. startfile returns as soon as the associated application is launched.
  313. There is no option to wait for the application to close, and no way
  314. to retrieve the application's exit status.
  315.  
  316. The filepath is relative to the current directory. If you want to use
  317. an absolute path, make sure the first character is not a slash ("/");
  318. the underlying Win32 ShellExecute function doesn't work if it is.
  319.  
  320. stat(...)
  321. #os.stat('path/filename') 获取文件/目录信息
  322. stat(path) -> stat result
  323.  
  324. Perform a stat system call on the given path.
  325.  
  326. stat_float_times(...)
  327. stat_float_times([newval]) -> oldval
  328.  
  329. Determine whether os.[lf]stat represents time stamps as float objects.
  330. If newval is True, future calls to stat() return floats, if it is False,
  331. future calls return ints.
  332. If newval is omitted, return the current setting.
  333.  
  334. strerror(...)
  335. strerror(code) -> string
  336.  
  337. Translate an error code to a message string.
  338.  
  339. system(...)#运行shell命令
  340. #os.system('cmd') #启动dos
  341. #启动cmd命令符
  342. Execute the command (a string) in a subshell.
  343.  
  344. tempnam(...)
  345. tempnam([dir[, prefix]]) -> string
  346.  
  347. Return a unique name for a temporary file.
  348. The directory and a prefix may be specified as strings; they may be omitted
  349. or None if not needed.
  350.  
  351. times(...)
  352. times() -> (utime, stime, cutime, cstime, elapsed_time)
  353.  
  354. Return a tuple of floating point numbers indicating process times.
  355.  
  356. tmpfile(...)
  357. tmpfile() -> file object
  358.  
  359. Create a temporary file with no directory entries.
  360.  
  361. tmpnam(...)
  362. tmpnam() -> string
  363.  
  364. Return a unique name for a temporary file.
  365.  
  366. umask(...)
  367. umask(new_mask) -> old_mask
  368.  
  369. Set the current numeric umask and return the previous umask.
  370.  
  371. unlink(...)
  372. unlink(path)
  373.  
  374. Remove a file (same as remove(path)).
  375.  
  376. urandom(...)
  377. urandom(n) -> str
  378.  
  379. Return n random bytes suitable for cryptographic use.
  380.  
  381. utime(...)
  382. utime(path, (atime, mtime))
  383. utime(path, None)
  384.  
  385. Set the access and modified time of the file to the given values. If the
  386. second form is used, set the access and modified times to the current time.
  387.  
  388. waitpid(...)
  389. waitpid(pid, options) -> (pid, status << 8)
  390.  
  391. Wait for completion of a given process. options is ignored on Windows.
  392.  
  393. walk(top, topdown=True, onerror=None, followlinks=False)#目录遍历
  394. Directory tree generator.
  395.  
  396. For each directory in the directory tree rooted at top (including top
  397. itself, but excluding '.' and '..'), yields a 3-tuple
  398. WindowsError: [Error 183] : '1'
  399. dirpath, dirnames, filenames
  400.  
  401. Example:
  402.  
  403. import os
  404. from os.path import join, getsize
  405. for root, dirs, files in os.walk('python/Lib/email'):
  406. print root, "consumes",
  407. print sum([getsize(join(root, name)) for name in files]),
  408. print "bytes in", len(files), "non-directory files"
  409. if 'CVS' in dirs:
  410. dirs.remove('CVS') # don't visit CVS directories
  411.  
  412. write(...)
  413. write(fd, string) -> byteswritten
  414.  
  415. Write a string to a file descriptor.
  416.  
  417. DATA:
  418. os.name#输出字符串指示正在使用的平台。如果是window 则用'nt'表示,对于Linux/Unix用户,它是'posix
  419. os.linesep字符串给出当前平台使用的行终止符 '\r\n' #Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'。
  420. os.curdir 返回当前目录: ('.')
  421. os.pardir 获取当前目录的父目录字符串名:('..')
  422. os.environ 获取系统环境变量
  423.  
  424. >>>
  425. '''

python之模块 os的更多相关文章

  1. Python文件属性模块Os.path

    Python文件属性模块Os.path介绍 os.path模块主要用于文件属性获取和判断,在编程中会经常用到,需要熟练掌握.以下是该模块的几种常用方法. os.path官方文档:http://docs ...

  2. python之模块(os、sys、json、subprocess)

    目录 os模块 sys模块 json模块 subprocess模块 os模块 os模块主要是与操作系统打交道. 导入os模块 import os 创建单层文件夹,路径必须要存在 os.mkdir(路径 ...

  3. Python标准模块--os

    1.模块简介 os模块主要包含普遍的操作系统相关操作,如果开发者希望自己开发的Python应用能够与平台无关,尤其需要关注os这个模块. 2.模块使用 2.1 os模块 1. os.name,输出字符 ...

  4. Python默认模块 os和shutil 实用函数

    os.sep 可以取代操作系统特定的路径分隔符.windows下为 '\\' os.name 字符串指示你正在使用的平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是 ' ...

  5. 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. ...

  6. Python常用模块os & sys & shutil模块

    OS模块 import os ''' os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录: ...

  7. python 操作系统模块 -- OS

    os,语义为操作系统,模块提供了访问多个操作系统服务的功能,可以处理文件和目录这些我们日常手动需要做的操作.os和它的子模块os.path还包括一些用于检查.构造.删除目录和文件的函数,以及一些处理路 ...

  8. python --- 23 模块 os sys pickle json

    一.   os模块 主要是针对操作系统的 用于文件操作 二.    sys 模块 模块的查找路径   sys.path 三.pickle 模块 1.  pickle.dumps(对象) 序列化  把对 ...

  9. python - 常用模块 os, sys

    常用模块: os(处理文件和目录), sys(sys 模块包含了与 Python 解释器和它的环境有关的函数.) sys.argv 变量是一个字符串的 列表.特别地,sys.argv 包含了 命令行参 ...

随机推荐

  1. Asp.Net Core 文件上传处理

    本文主要介绍后台接收处理 1.在使用控制器接收 : [HttpPost] : public IActionResult UploadFiles(IList<IFormFile> files ...

  2. .Net Core中文编码问题整理

    1..Net Core Console控制台程序 在.Net Core中默认System.Text中不支持CodePagesEncodingProvider.Instance, System.Text ...

  3. protobuf 语法简介

    protobuf 语法简介 1.基本语义 在.proto文件中,最基本的数据类型为message,如其定义所示,由message引导,之后是message类型的名字,之后是由{}包含的各个域(fiel ...

  4. BZOJ3328: PYXFIB

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3328 题解:关键在于只处理i%k的项,那么我们就需要用一个式子来表达这个东西. p%k==1. ...

  5. go语言之进阶篇通过range遍历channel内容

    1.通过range遍历channel内容 package main import ( "fmt" ) func main() { ch := make(chan int) //创建 ...

  6. svg image标签降级技术

    1.svg image标签降级技术: svg不能很好的在anroid2.3中得到支持,需要额外的补充,IE8-以及Android 2.3默认浏览器是不支持SVG的. svg image标签降级技术,这 ...

  7. [转]0.python:scikit-learn基本用法

    感谢百小度治哥,该文原地址:here 经Edwin Chen的推荐,认识了scikit-learn这个非常强大的python机器学习工具包.这个帖子作为笔记.(其实都没有笔记的意义,因为他家文档做的太 ...

  8. C#遍历可变化的集合

    如果用foreach,会造成被遍历的集合更改后带来异常问题. 方法一:用for循环可有效的解决这个问题. ;i<List.Count;i++) { if(条件是真) { List.Remove( ...

  9. Nginx配置基于多域名、端口、IP的虚拟主机

    原文:https://www.cnblogs.com/ssgeek/p/9220922.html ------------------------------- Nginx配置基于多域名.端口.IP的 ...

  10. C++结构变量数据对齐问题

    为了避免混淆.做例如以下规定,下面代码若不加特殊说明都执行于32位平台,结构体的默认对齐值是8,各数据类型所占字节数分别为 char占一个字节 int占四个字节 double占八个字节. 两个样例 请 ...