Python——系统(os)

文件

Python在模块os中提供了许多系统函数。

用open()创建文件

with open('os_file','rt') as os_file:
    print(os_file.read())

用exists()检查文件是否存在

要判断文件或者目录是否存在,可以使用exists(),传入相对或者绝对路径名

import os

print(os.path.exists('os_file'))
>>> True
print(os.path.exists('os_file2'))
>>> False

用isfile()检查是否为文件

print(os.path.isfile('os_file'))
>>> True 
print(os.path.isfile('test_dir'))
>>> False
  • 文件路径:一个.号表示当前目录,两个点号..表示上层目录
  • isabs(name) 可以判断参数是否是一个绝对路径名

用isdir(name)判断是否是目录

print(os.path.isdir('test_dir'))

用copy()复制文件

copy()函数来自于另一个模块shutil

  • copy(sourcefile,destfile) 返回复制的文件名
import shutil

if os.path.exists('os_file'):
    # 复制文件
    print(shutil.copy('os_file', 'os_file_copy'))

用move()复制文件

  • move(src,des) 使用move()函数会复制一个文件并删除原始文件 并返回移动的目标文件文件名
print(shutil.move('os_file_copy', 'os_file_mv'))

用rename()重命名文件

  • rename(srcname,newname) 重命名文件
print(os.rename('os_file_mv', 'os_file_rname'))

用link或者symlink创建链接

在Unix中,文件只存在于一个位置,但是可以有多个名称,这种机制叫做链接。

  • link()会创建一个硬链接
  • symlink()会创建一个符号链接
  • isLink()函数会检查参数是文件还是符号链接

用chmod()修改权限

  • chmod(src,0o权限) chmod()可以修改文件权限。该函数接收两个参数 1个是文件路径 另一个是压缩过的八进制,还可以使用从stat模块中导入的常量
print(os.chmod('os_file_rname', stat.S_IRUSR))

用chown()修改所有者

  • chown(file,uid,gid)
uid = 5
gid = 6
print(os.chown('os_file', uid, gid))

用abspath()获取路径名

  • abspath(filepath) 这个函数会把一个相对路径名扩展成绝对路径名
print(os.path.abspath('os_file'))

用remove()删除文件

  • rename(filepath) 删除指定的文件
os.remove('os_file_rname')
print(os.path.exists('os_file_rname'))

目录

使用mkdir()创建目录

  • mkdir(dirpath) 创建目录
import os
os.mkdir('osk_dir')
print(os.path.exists('osk_dir'))
print(os.path.isdir('osk_dir'))

使用rmdir()删除目录

  • rmdir(dirpath) 删除指定路径的目录
print(os.rmdir('osk_dir'))

使用listdir()列出目录内容

  • lsdir(dirpath) 列出指定目录的内容 返回目录内容的列表,默认不传参数为当前目录
print(type(os.listdir('.')))

使用chdir()修改当前目录

  • chidir(path) 我们可以使用这个函数从一个目录跳转到另一个目录
print(os.chdir('..'))
print(os.listdir())

程序和进程

当运行一个程序时,操作系统会创建一个进程。它会使用系统资源(CPU、内存、磁盘空间)和操作系统内核中的数据结构(文件、网络连接、用量统计等)。进程之间是相互隔离的,一个进程既无法访问其他进程,也无法操作其他进程

获取Python解释器的进程号和当前工作目录

import os
# 获取Python的进程号
print(os.getpid())
# 获取当前工作目录
print(os.getcwd())

获取我的用户ID和用户组ID


# 获取用户id
print(os.getuid())
# 获取用户组id
print(os.getegid())

使用subprocess创建进程

可以使用Python标准库中的subprocess模块来启动和终止其他程序

  • getoutput() 使用shell命令运行其他程序并获取它的输出
import subprocess

ret = subprocess.getoutput('date')
print(ret)
  • check_output() 可以接受一个命令和参数列表

使用multiprocessing创建进程

可以使用multiprocessing木块在一个程序中运行多个进程

创建多个进程

import multiprocessing
import os


def whoani(what):
    print('process id %s what %s' % (os.getpid(), what))


def do_this(what):
    whoani(what)


if __name__ == '__main__':
    whoani('i am main')
    for n in range(5):
        p = multiprocessing.Process(target=do_this, args=("I'm function %d" % n,))
        p.start()
# >>>
process id 6597 what i am main
process id 6598 what I'm function 0
process id 6599 what I'm function 1
process id 6600 what I'm function 2
process id 6601 what I'm function 3
process id 6602 what I'm function 4

使用terminate()终止进程

  • 调用方式
if __name__ == '__main__':
    whoani('i am main')
    for n in range(5):
        p = multiprocessing.Process(target=do_this, args=("I'm function %d" % n,))
        p.start()
        if n== 2:
            time.sleep(2000)
            p.terminate()
# 输出
process id 6616 what i am main
process id 6617 what I'm function 0
process id 6618 what I'm function 1
process id 6619 what I'm function 2
可见 往后的进程不会创建了
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 14,228评论 2 33
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,798评论 19 139
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 99,973评论 9 468
  • php usleep() 函数延迟代码执行若干微秒。 unpack() 函数从二进制字符串对数据进行解包。 uni...
    思梦PHP阅读 6,138评论 1 24
  • “写作”二字实在不敢当,但是为了让标题耸动一些还是厚着脸皮这么写吧。宅男的内心总是有太多想说的,苦于找不到说的对象...
    尬聊终结者阅读 1,917评论 5 4