Python module: OS module

Source: Internet
Author: User

#!/bin/env python
#!-*-Coding:utf-8-*-

Import OS
Print Os.name #输出主机平台
Print OS.GETCWD () #输出当前目录
Print Os.listdir (OS.GETCWD ()) #输出当前目录的文件 (Landscape)

For I in Os.listdir (OS.GETCWD ()): #输出当前目录的文件 (Portrait)
Print I

Os.makedirs ("/tmp/tong/123") #创建多级目录
Os.mkdir ("/tmp/123") #创建单级目录
Os.chdir ("/tmp/123") #进入指定的文件夹
Os.chown ("/tmp/tong", 0,0) #修改文件所组主和所属组, 0 indicates UID and GID
Os.chmod ("/tmp/tong", 0777) #修改文件的权限
Os.listdir ("/tmp") #列出所有文件或目录
OS.GETCWD () #查看当前目录
Os.getegid () #返回当前用户组的GID
Os.setegid (0) #设置当前进程组的GID
Os.geteuid () #返回当前用户的UID
Os.seteuid (0) #设置当前进程的GID
Os.getpid () #返回当前进程的PID
Os.setpid () #设置当前进程的PID
Os.getppid () #返回当前进程的PID的子进程
Os.getsid () #返回系统ID
Os.getuid () #返回用户ID
Os.getgroups () #返回当前用户所属组
Os.getpgid (1256) #返回gid的进程
OS.GETPGRP (1256) #返回组的进程

Os.lstat ("/tmp/123") #返回文件的状态 (soft link)
Os.stat ("/tmp/123") #返回文件的状态
Os.remove ("/tmp/tong/12.txt") #删除文件
Os.removedirs ("/tmp/tong") #删除文件夹
Os.rename ("/homt/1.txt", "Home/2.txt") #重命名文件
Os.rmdir ("/home/123") #删除目录
Os.umask (0777) #指定目录的mask值
Os.uname () #主机信息
Os.getlogin () #当前登陆的用户
Os.system ("ls") #运行shell命令, LS is a shell command
Os.kill (1256,15) #杀死进程
Os.pardir #返回当前目录的你目录, equivalent to (..), there are many options below the command
Os.curdir #返回当前目录, equivalent to (.), there are many options below the command
Os.environ #输出系统环境变量

Os.tmpfile ("/tmp/tong/tong1.txt") #判断文件是否是临时文件
Os.path.split ("/tmp/tong/tong1.txt") #返回目录和文件名
Os.path.isabs ("/tmp/tong/tong1.txt") #判断路径是否是绝对路径
Os.path.isdir ("/tmp/tong/tong1.txt") #判断路径是否是目录
Os.path.isfile ("/tmp/tong/tong1.txt") #判断路径是否是文件
Os.path.islink ("/tmp/tong/tong1.txt") #判断路径是否是链接文件
Os.path.ismount ("/tmp/tong/tong1.txt") #判断路径是否是挂载文件
Os.path.basename ("/tmp/tong/tong1.txt") #返回文件名
Os.path.dirname ("/tmp/tong/tong1.txt") #返回路径名
Os.path.exists ("/tmp/tong/tong1.txt") #判断文件是否存在
Os.path.getatime ("/tmp/tong/tong1.txt") #返回文件访问时间
Os.path.getctime ("/tmp/tong/tong1.txt") #返回文件创建时间
Os.path.getmtime ("/tmp/tong/tong1.txt") #返回文件修改时间
Os.path.getsize ("/tmp/tong/tong1.txt") #返回文件的大小
Os.path.splitext ("/tmp/tong/tong1.txt") #分离文件名和文件后缀
Os.path.json ("/tmp/tong", "12.txt") #连接目录和文件, or directories and directories


This article from "Together Through the Days" blog, declined reprint!

Python module: OS module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.