ddxiami

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
楼主: 黄振国

基础语法06--OS

[复制链接]
 楼主| 发表于 2020-4-17 15:28:49 | 显示全部楼层
#Python3 os.fchmod() 方法
用于改变一个文件的访问权限,该文件由参数fd指定,参数mode是Unix下的文件访问权限。Unix上可用。

====================
#!/usr/bin/python3

import os, sys, stat

# 打开文件 "/tmp/foo.txt"
fd = os.open( "/tmp", os.O_RDONLY )

# 设置文件可通过组执行

os.fchmod( fd, stat.S_IXGRP)

# 设置文件可被其他用户写入
os.fchmod(fd, stat.S_IWOTH)

print ("修改权限成功!!")

# 关闭文件
os.close( fd )
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-17 15:30:27 | 显示全部楼层
#Python3 os.fchown() 方法
#用于修改一个文件的所有权,这个函数修改一个文件的用户ID和用户组ID,该文件由文件描述符fd指定。Unix上可用。

====================
#!/usr/bin/python3

import os, sys, stat

# 打开文件 "/tmp/foo.txt"
fd = os.open( "/tmp", os.O_RDONLY )

# 设置文件的用户 id 为 100
os.fchown( fd, 100, -1)

# 设置文件的用户组 id 为 50
os.fchown( fd, -1, 50)


print ("修改权限成功!!")

# 关闭文件
os.close( fd )
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-17 15:31:51 | 显示全部楼层
#Python3 os.fdatasync() 方法
#用于强制将文件写入磁盘,该文件由文件描述符fd指定,但是不强制更新文件的状态信息。如果你需要刷新缓冲区可以使用该方法。Unix上可用。

====================
#!/usr/bin/python3

import os, sys

# 打开文件 "/tmp/foo.txt"
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 写入字符串
os.write(fd, "This is test".encode())

# 使用 fdatasync() 方法
os.fdatasync(fd)

# 读取文件
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print ("读取的字符是 : ", str)

# 关闭文件
os.close( fd )

print ("关闭文件成功!!")
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-17 15:34:38 | 显示全部楼层
#Python3 os.fdopen() 方法
#用于通过文件描述符 fd 创建一个文件对象,并返回这个文件对象。该方法是内置函数 open() 的别名,可以接收一样的参数,唯一的区别是 fdopen() 的第一个参数必须是整型。

====================
#!/usr/bin/python3

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 获取以上文件的对象
fo = os.fdopen(fd, "w+")

# 获取当前文章
print ("Current I/O pointer position :%d" % fo.tell())

# 写入字符串
fo.write( "Python is a great language.\nYeah its great!!\n");

# 读取内容
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print ("Read String is : ", str)

# 获取当前位置
print ("Current I/O pointer position :%d" % fo.tell())

# 关闭文件
os.close( fd )

print ("关闭文件成功!!")
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-17 15:39:41 | 显示全部楼层
#Python3 os.fpathconf() 方法
#用于返回一个打开的文件的系统配置信息。Unix上可用。

====================
#!/usr/bin/python3

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

print ("%s" % os.pathconf_names)

# 获取最大文件连接数
no = os.fpathconf(fd, 'PC_LINK_MAX')
print ("文件最大连接数为 :%d" % no)

# 获取文件名最大长度
no = os.fpathconf(fd, 'PC_NAME_MAX')
print ("文件名最大长度为 :%d" % no)

# 关闭文件
os.close( fd )

print ("关闭文件成功!!")
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-17 15:45:21 | 显示全部楼层
#Python3 os.fstat() 方法
#用于返回文件描述符fd的状态,类似 stat()。

====================
#!/usr/bin/python3

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 获取元组
info = os.fstat(fd)

print ("文件信息 :", info)

# 获取文件 uid
print ("文件 UID :%d" % info.st_uid)

# 获取文件 gid
print ("文件 GID  :%d" % info.st_gid)

# 关闭文件
os.close( fd)

#参数
====================
st_dev: 设备信息
st_ino: 文件的i-node值
st_mode: 文件信息的掩码,包含了文件的权限信息,文件的类型信息(是普通文件还是管道文件,或者是其他的文件类型)
st_nlink: 硬连接数
st_uid: 用户ID
st_gid: 用户组 ID
st_rdev: 设备 ID (如果指定文件)
st_size: 文件大小,以byte为单位
st_blksize: 系统 I/O 块大小
st_blocks: 文件的是由多少个 512 byte 的块构成的
st_atime: 文件最近的访问时间
st_mtime: 文件最近的修改时间
st_ctime: 文件状态信息的修改时间(不是文件内容的修改时间)
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-17 15:47:27 | 显示全部楼层
#Python3 os.fstatvfs() 方法
#用于返回包含文件描述符fd的文件的文件系统的信息,Python 3.3 相等于 statvfs()。。Unix上可用。

====================
#!/usr/bin/python3

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 获取元组
info = os.fstatvfs(fd)

print ("文件信息 :", info)

# 获取文件名最大长度
print ("文件名最大长度 :%d" % info.f_namemax)

# 获取可用块数
print ("可用块数 :%d" % info.f_bfree)

# 关闭文件
os.close( fd)

#参数
====================
f_bsize: 文件系统块大小
f_frsize: 分栈大小
f_blocks: 文件系统数据块总数
f_bfree: 可用块数
f_bavail:非超级用户可获取的块数
f_files: 文件结点总数
f_ffree: 可用文件结点数
f_favail: 非超级用户的可用文件结点数
f_fsid: 文件系统标识 ID
f_flag: 挂载标记
f_namemax: 最大文件长度
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-17 15:49:05 | 显示全部楼层
#Python3 os.fsync() 方法
#强制将文件描述符为fd的文件写入硬盘。在Unix, 将调用fsync()函数;在Windows, 调用 _commit()函数。
====================
#!/usr/bin/python3

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 写入字符串
os.write(fd, "This is test".encode())

# 使用 fsync() 方法.
os.fsync(fd)

# 读取内容
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print ("读取的字符串为 : ", str)

# 关闭文件
os.close( fd)

print ("关闭文件成功!!")
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-17 15:51:00 | 显示全部楼层
#Python3 os.ftruncate() 方法
#裁剪文件描述符fd对应的文件, 它最大不能超过文件大小。Unix上可用。

====================
#!/usr/bin/python3

import os, sys

# 打开文件
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )

# 写入字符串
os.write(fd, "This is test - This is test".encode())

# 使用 ftruncate() 方法
os.ftruncate(fd, 10)

# 读取内容
os.lseek(fd, 0, 0)
str = os.read(fd, 100)
print ("读取的字符串是 : ", str)

# 关闭文件
os.close( fd)


回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-17 16:48:46 | 显示全部楼层
#Python3 os.getcwd() 方法
#用于返回当前工作目录。

====================
#!/usr/bin/python3

import os, sys

# 切换到 "/var" 目录
os.chdir("/var" )

# 打印当前目录
print ("当前工作目录 : %s" % os.getcwd())

# 打开 "/tmp"
fd = os.open( "/tmp", os.O_RDONLY )

# 使用 os.fchdir() 方法修改目录
os.fchdir(fd)

# 打印当前目录
print ("当前工作目录 : %s" % os.getcwd())

# 关闭文件
os.close( fd )
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|技术文档库 ( 闽ICP备15017263号-2 )|网站地图

GMT+8, 2025-5-18 23:40 , Processed in 0.035369 second(s), 13 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表