python关于os模块

1.介绍:
os模块用于python文件和操作系统的交互
2.os模块函数
(1)

os.path.abspath(path)
Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to calling the function normpath() as follows: normpath(join(os.getcwd(), path)).
返回规范化的绝对路径,在大多数平台上, 相当于调用函数normpath()

print(os.path.abspath(__file__))
#D:\pythonfiles\exercise\ftp\ftp_server\src\sign.py

(2)

os.path.dirname(path)
Return the directory name of pathname path. This is the first element of the pair returned by passing path to the function split().
返回路径的目录名, 这是os.split()函数返回值的第一个元素

BaseDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BaseDir)
#D:\pythonfiles\exercise\ftp\ftp_server

(3)

os.path.join(path, *paths)
1)Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
2)On Windows, the drive letter is not reset when an absolute path component (e.g., r’\foo’) is encountered. If a component contains a drive letter, all previous components are thrown away and the drive letter is reset. Note that since there is a current directory for each drive, os.path.join(“c:”, “foo”) represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

智能拼接一个或多个路径, 返回值是path和*path的串联路径, 除了最后一个以外, 在每个非空参数间用一个路径分割符连接, 如果最后一个参数是空, 那么最终返回路径将以路径分割符结尾. 另外, 如果路径中有一个是绝对路径, 那它之前的参数将全部被丢弃, 从这个绝对路径开始拼接.

在Windows系统中, 如果遇到一个绝对路径,驱动器符号不会重置 ,但如果路径中包含了驱动器符, 前面的路径会被丢弃并且那驱动器会被重置.另外注意盘符直接和目录拼接的情况: os.path.join(“d:”, “foo”) 返回d:foo

使用示例:

path = os.path.join(BaseDir, 'conf', 'user_info.conf')
print(path)  #  拼接示例
#D:\pythonfiles\exercise\ftp\ftp_server\conf\user_info.conf
path1 = os.path.join(BaseDir, '\\conf', 'user_info.conf', '')
print(path1)  
#第二个路径为绝对路径, 因此第一个被丢弃, 但盘符未重置,最后一个路径为空,所以返回路径以路径分割符结尾
#D:\conf\user_info.conf\

(4)

os.listdir(path=’.’)
1)Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries ‘.’ and ‘…’ even if they are present in the directory.
2)path may be a path-like object. If path is of type bytes (directly or indirectly through the PathLike interface), the filenames returned will also be of type bytes; in all other circumstances, they will be of type str.
3)This function can also support specifying a file descriptor; the file descriptor must refer to a directory.
Note:
To encode str filenames to bytes, use fsencode().
See also:
The scandir() function returns directory entries along with file attribute information, giving better performance for many common use cases.

返回一个列表, 包含路径目录下的文件名, 注意:列表的顺序是任意排列的,不包括’ . ‘和’ … '入口;
如果path是bytes形式,那么返回的文件名也是bytes形式;
如果使用os.scandir()可能会有更好的表现;
其余待补充

print(os.listdir(os.path.dirname(os.path.abspath(__file__))))
print(os.listdir())  # 默认为当前路径
#['test.py', 'user_info.conf']
#['test.py', 'user_info.conf']

os.scandir使用示例:

a = os.scandir()  # 返回一个os.DirEntry对象的迭代器,path默认为' . '
print(a)  # 但是没有next()方法???
for i in a:  # 使用方法
    print(i.name)
    print(i.path)  # 是否为绝对路径取决于os.scandir的路径是否为绝对
    print(i.inode())
    print(i.is_file(), i.is_dir())
    print(i.stat())
#result
<nt.ScandirIterator object at 0x00000112AC3D0270>
test.py
.\test.py
9288674231720971
True False
os.stat_result(st_mode=33206, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=868, st_atime=1541654991, st_mtime=1541654991, st_ctime=1541654991)
user_info.conf
.\user_info.conf
3377699720797180
True False
os.stat_result(st_mode=33206, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=149, st_atime=1541646585, st_mtime=1541642663, st_ctime=1541642663)

(5)

os.chdir(path)
Change the current working directory to path.
This function can support specifying a file descriptor. The descriptor must refer to an opened directory, not an open file.
New in version 3.3: Added support for specifying path as a file descriptor on some platforms.

改变目前的工作目录

(6)

os.path.exists(path)
Return True if path refers to an existing path or an open file descriptor. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.

(7)

os.path.isfile(path)
Return True if path is an existing regular file. This follows symbolic links, so both islink() and isfile() can be true for the same path.

os.path.isdir(path)
Return True if path is an existing directory. This follows symbolic links, so both islink() and isdir() can be true for the same path.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值