From 6acab007f605f2f58c2ab5aa901d200184f43e90 Mon Sep 17 00:00:00 2001 From: XiongNeng Date: Wed, 24 Sep 2014 00:20:03 +0800 Subject: [PATCH] =?UTF-8?q?5.12=E5=B0=8F=E8=8A=82=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cookbook/c05/p12_file_existence.py | 27 ++++++++ .../p12_test_for_the_existence_of_file.rst | 66 ++++++++++++++++++- 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 cookbook/c05/p12_file_existence.py diff --git a/cookbook/c05/p12_file_existence.py b/cookbook/c05/p12_file_existence.py new file mode 100644 index 0000000..9f9b48d --- /dev/null +++ b/cookbook/c05/p12_file_existence.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +""" +Topic: 测试文件或目录是否存在 +Desc : +""" +import os +import time + + +def file_existence(): + print(os.path.exists('/etc/passwd')) + print(os.path.exists('/tmp/spam')) + + print(os.path.isfile('/etc/passwd')) + print(os.path.isdir('/etc/passwd')) + print(os.path.islink('/usr/local/bin/python3')) + print(os.path.realpath('/usr/local/bin/python3')) + + print(os.path.getsize('/etc/passwd')) + print(os.path.getmtime('/etc/passwd')) + print(time.ctime(os.path.getmtime('/etc/passwd'))) + + +if __name__ == '__main__': + file_existence() + diff --git a/source/c05/p12_test_for_the_existence_of_file.rst b/source/c05/p12_test_for_the_existence_of_file.rst index 75bb7ae..f0329bf 100644 --- a/source/c05/p12_test_for_the_existence_of_file.rst +++ b/source/c05/p12_test_for_the_existence_of_file.rst @@ -5,14 +5,74 @@ ---------- 问题 ---------- -todo... +你想测试一个文件或目录是否存在。 + +| ---------- 解决方案 ---------- -todo... +使用 ``os.path`` 模块来测试一个文件或目录是否存在。比如: + +.. code-block:: python + + >>> import os + >>> os.path.exists('/etc/passwd') + True + >>> os.path.exists('/tmp/spam') + False + >>> + +你还能进一步测试这个文件时什么类型的。 +在下面这些测试中,如果测试的文件不存在的时候,结果都会返回False: + +.. code-block:: python + + >>> # Is a regular file + >>> os.path.isfile('/etc/passwd') + True + + >>> # Is a directory + >>> os.path.isdir('/etc/passwd') + False + + >>> # Is a symbolic link + >>> os.path.islink('/usr/local/bin/python3') + True + + >>> # Get the file linked to + >>> os.path.realpath('/usr/local/bin/python3') + '/usr/local/bin/python3.3' + >>> + +如果你还想获取元数据(比如文件大小或者是修改日期),也可以使用 ``os.path`` 模块来解决: + +.. code-block:: python + + >>> os.path.getsize('/etc/passwd') + 3669 + >>> os.path.getmtime('/etc/passwd') + 1272478234.0 + >>> import time + >>> time.ctime(os.path.getmtime('/etc/passwd')) + 'Wed Apr 28 13:10:34 2010' + >>> + +| ---------- 讨论 ---------- -todo... \ No newline at end of file +使用 ``os.path`` 来进行文件测试是很简单的。 +在写这些脚本时,可能唯一需要注意的就是你需要考虑文件权限的问题,特别是在获取元数据时候。比如: + +.. code-block:: python + + >>> os.path.getsize('/Users/guido/Desktop/foo.txt') + Traceback (most recent call last): + File "", line 1, in + File "/usr/local/lib/python3.3/genericpath.py", line 49, in getsize + return os.stat(filename).st_size + PermissionError: [Errno 13] Permission denied: '/Users/guido/Desktop/foo.txt' + >>> +