8.1.4 具体路径类

来源:互联网 发布:软件需求管理工具 编辑:程序博客网 时间:2024/06/04 19:59

具体路径类是纯路径类的子类,后面提供了一些操作方法,以及一些系统调用。创建具体路径类主要有三个方法创建:

class pathlib.Path(*pathsegments) 

构造一个PurePath类的子类,它表示具体的路径类。

例子:

#python 3.4

from pathlib import *

 

p = Path('/a/b.py')

print(p.with_suffix('.7z'))

print(p.with_suffix('.zip'))

结果输出如下:

\a\b.7z

\a\b.zip

 

class pathlib.PosixPath(*pathsegments) 

创建一个非Windows系统的具体路径。

例子:

#python 3.4

from pathlib import *

 

p = PosixPath('/a/b.py')

Windows系统下是不能调用此函数来创建对象,出错信息如下:

Traceback (most recent call last):

  File "F:\temp\path1.py", line 4, in <module>

    p = PosixPath('/a/b.py')

  File "C:\Python34\lib\pathlib.py", line 911, in __new__

    % (cls.__name__,))

NotImplementedError: cannot instantiate 'PosixPath' on your system

这就是与纯路径类的区别,在前面的例子里是可以在Windows系统调用纯路径类来创建的。

 

class pathlib.WindowsPath(*pathsegments) 

创建一个Windows下的具体类,它是PureWindowsPath的子类。

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('/a/b.py')

print(p.with_suffix('.7z'))

print(p.with_suffix('.zip'))

结果输出如下:

\a\b.7z

\a\b.zip

 

classmethod Path.cwd() 

返回一个新的路径对象,表示当前路径。

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('/a/b.py')

print(p.cwd())

结果输出如下:

F:\temp\pywin

 

Path.stat() 

返回路径相关信息,主要的功能与os.stat()函数一样,具体参数与系统里实现stat()函数一样。

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

print(p.stat().st_size)

print(p.stat().st_mtime)

结果输出如下:

104

1450406421.5051708

 

Path.chmod(mode) 

改变文件的模式和权限。

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

print(p.stat().st_size)

print(p.stat().st_mtime)

print(p.stat().st_mode)

p.chmod(0o444)

print(p.stat().st_mode)

结果输出如下:

104

1450406421.5051708

33206

33060

 

Path.exists() 

判断路径或者文件是否存在。

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

print(p.exists())

结果输出如下:

True

 

Path.glob(pattern) 

对一个给出的路径进行模式匹配,列出所有匹配的文件。

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py')

print(p.glob('*.py'))

print(list(p.glob('*.py')))

结果输出如下:

<generator object glob at 0x02D1A9B8>

[WindowsPath('F:/temp/py/bisect1.py'), WindowsPath('F:/temp/py/cal_1.py'), WindowsPath('F:/temp/py/chainmap1.py'), WindowsPath('F:/temp/py/chainmap2.py'), WindowsPath('F:/temp/py/closescreen.py'), WindowsPath('F:/temp/py/codecs1.py'), WindowsPath('F:/temp/py/complex1.py'), WindowsPath('F:/temp/py/copy1.py'), WindowsPath('F:/temp/py/datetimetz.py'), WindowsPath('F:/temp/py/dec1.py'), WindowsPath('F:/temp/py/difflib1.py'), WindowsPath('F:/temp/py/difflib2.py'), WindowsPath('F:/temp/py/difflib3.py'), WindowsPath('F:/temp/py/difflib4.py'), WindowsPath('F:/temp/py/difflib5.py')]

 

Path.group() 

返回路径指定文件所属的组名称。在Windows下不能使用。

 

Path.is_dir() 

如果路径是一个目录则返回True,否则返回False

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

print(p.is_dir())

结果输出如下:

False

 

Path.is_file() 

如果路径是一个目录则返回False,如果路径是一个文件则返回True

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

print(p.is_file())

结果输出如下:

True

 

Path.is_symlink()

判断路径是否一个符号连接,如果是返回True,否则返回False

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

print(p.is_symlink())

结果输出如下:

False

 

Path.is_socket()

如果指向的文件路径是Unixsocket,返回True,否则返回False

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

print(p.is_socket())

结果输出如下:

False

 

Path.is_fifo() 

判断路径是否指向一个FIFO文件,如果是返回True,否则返回False

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

print(p.is_fifo())

结果输出如下:

False

 

Path.is_block_device() 

如果路径指向的设备是块设备返回True,否则返回False

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

print(p.is_block_device())

结果输出如下:

False

 

Path.is_char_device() 

如果路径指向字符设备返回True,否则返回False

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

print(p.is_char_device())

结果输出如下:

False

 

Path.iterdir() 

枚举整个目录项。

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\')

for child in p.iterdir():

    print(child)

结果输出如下:

F:\temp\py\bisect1.py

F:\temp\py\cal_1.py

F:\temp\py\chainmap1.py

F:\temp\py\chainmap2.py

F:\temp\py\closescreen.py

F:\temp\py\codecs1.py

F:\temp\py\complex1.py

F:\temp\py\copy1.py

F:\temp\py\datetimetz.py

F:\temp\py\dec1.py

F:\temp\py\difflib1.py

F:\temp\py\difflib2.py

F:\temp\py\difflib3.py

F:\temp\py\difflib4.py

F:\temp\py\difflib5.py

 

Path.lchmod(mode) 

本函数的功能与Path.chmod()是一样的,不过它的路径是一个符号连接时,只改变符号连接的属性,而不是改变符号连接的目标文件的属性。

Path.lstat() 

本函数的功能与Path.stat()是一样的,不过它的路径是一个符号连接时,只返回符号连接的属性,而不是返回符号连接的目标文件的属性。

Path.mkdir(mode=0o777, parents=False) 

按路径的内容来创建目录。如果参数parentsTrue,父目录路径为空的,全部创建。如果参数parentsFalse,父目录不存在时会抛出异常FileNotFoundError

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\testdir')

p.mkdir()

 

Path.open(mode='r', buffering=-1, encoding=None, errors=None, newline=None) 

打开路径指定的文件。其它参数与内置函数open()参数一样。

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

with p.open() as f:

    print(f.readline())

结果输出如下:

#python 3.4

 

Path.owner() 

返回路径的拥有者。

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

print(p.owner())

本函数在Windows下不可以使用,会抛出异常。

 

Path.rename(target) 

把当前路径重新命名为参数target的路径。

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\bisect1.py')

x = WindowsPath('F:\\temp\\py\\bisect2.py')

print(p.rename(x))

在这里把文件'F:\\temp\\py\\bisect1.py'重新改名称为'F:\\temp\\py\\bisect2.py'

 

Path.replace(target) 

把路径指向的文件或目录无条件替换为参数target指定的名称。

 

Path.resolve() 

把路径进行处理,返回绝对路径的形式,比如路径中包括点号或者双点号的情况下,就把它们删除掉,返回正确的绝对路径。如果路径处理之后,发现不存在就会抛出异常FileNotFoundError

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\..\\bisect2.py')

print(p.resolve())

结果输出如下:

F:\temp\bisect2.py

 

Path.rglob(pattern) 

本函数实现的功能与glob()函数是一样的,主要差别是在参数pattern之前添加了“**”模式。

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('F:\\temp\\py\\')

print(sorted(p.rglob('*.py')))

结果输出如下:

[WindowsPath('F:/temp/py/bisect2.py'), WindowsPath('F:/temp/py/cal_1.py'), WindowsPath('F:/temp/py/chainmap1.py'), WindowsPath('F:/temp/py/chainmap2.py'), WindowsPath('F:/temp/py/closescreen.py'), WindowsPath('F:/temp/py/codecs1.py'), WindowsPath('F:/temp/py/complex1.py'), WindowsPath('F:/temp/py/copy1.py'), WindowsPath('F:/temp/py/datetimetz.py'), WindowsPath('F:/temp/py/dec1.py'), WindowsPath('F:/temp/py/difflib1.py'), WindowsPath('F:/temp/py/difflib2.py'), WindowsPath('F:/temp/py/difflib3.py'), WindowsPath('F:/temp/py/difflib4.py'), WindowsPath('F:/temp/py/difflib5.py')]

 

Path.rmdir() 

删除指定路径的目录。要删除的目录必须为空。

Path.symlink_to(target, target_is_directory=False)

创建一个路径连接到一个目标文件。在Windows系统下,当创建一个目录的快捷方式时,参数target_is_directory必须True;在Unix下,这个参数忽略掉。

例子:

#python 3.4

from pathlib import *

 

p = WindowsPath('f:\mylink')

p.symlink_to('F:\\temp\\py\\', target_is_directory = True)

这样就可以把'f:\mylink'的快捷方式指向目录'F:\\temp\\py\\',当双击目录'f:\mylink'就会打开目录'F:\\temp\\py\\'

 

Path.touch(mode=0o777, exist_ok=True) 

给出一个路径创建一个文件。文件使用参数mode作为属性。如果参数exist_ok设置为True,当文件名称已经存,也会创建文件,否则会抛出异常FileExistsError

 

Path.unlink() 

删除给出路径的文件或者快捷方式。如果给出的是目录,需要使用rmdir()函数来删除。

 



蔡军生 QQ:9073204  深圳

1 0
原创粉丝点击