检查文件是否存在的最佳方法? [英] best way to check if a file exists?

查看:85
本文介绍了检查文件是否存在的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检查当前

目录中是否已存在文件的最佳方法是什么?我看到了os.path.isfile(),但我不确定它是否比我需要的更多




我只是想检查用户之前是否存在某个名称的文件

创建一个具有该名称的新文件。


谢谢。

What is the best way to check if a file already exists in the current
directory? I saw os.path.isfile(), but I''m not sure if that does more
than what I need.

I just want to check if a file of a certain name exists before the user
creates a new file of that name.

Thanks.

推荐答案



John Salerno写道:

John Salerno wrote:

检查是否有什么最好的方法文件已经存在于当前的

目录中?我看到了os.path.isfile(),但我不确定它是否比我需要的更多




我只是想检查用户之前是否存在某个名称的文件

创建一个具有该名称的新文件。


谢谢。
What is the best way to check if a file already exists in the current
directory? I saw os.path.isfile(), but I''m not sure if that does more
than what I need.

I just want to check if a file of a certain name exists before the user
creates a new file of that name.

Thanks.



os.path.exists()?

os.path.exists()?


2006年10月31日星期二18:01,John Salerno写道:
At Tuesday 31/10/2006 18:01, John Salerno wrote:

>检查当前
目录中是否存在文件的最佳方法是什么?我看到了os.path.isfile(),但我不确定它是否比我需要的更多。
>What is the best way to check if a file already exists in the current
directory? I saw os.path.isfile(), but I''m not sure if that does more
than what I need.



os.access(full_filename,os.F_OK)
http://docs.python.org/lib/os-file-dir.html


>我只是想在用户创建一个具有该名称的新文件之前检查某个名称的文件是否存在。
>I just want to check if a file of a certain name exists before the user
creates a new file of that name.



请记住,检查名称和你之间的事情可能会发生变化

实际创建文件。

- -

Gabriel Genellina

Softlab SRL

__________________________________________________

Correo Yahoo!

Espacio para todos tus mensajes,antivirus y antispam?gratis!

?Abrítucuenta ya! - http://correo.yahoo.com.ar


10月31日下午4:01,John Salerno< johnj ... @ NOSPAMgmail.com写道:
On Oct 31, 4:01 pm, John Salerno <johnj...@NOSPAMgmail.comwrote:

什么是检查当前

目录中是否已存在文件的最佳方法?我看到了os.path.isfile(),但我不确定它是否比我需要的更多




我只是想检查用户之前是否存在某个名称的文件

创建该名称的新文件。
What is the best way to check if a file already exists in the current
directory? I saw os.path.isfile(), but I''m not sure if that does more
than what I need.

I just want to check if a file of a certain name exists before the user
creates a new file of that name.



您可以更多pythonic,并且只是尝试创建文件,

如果失败则捕获异常。这适用于linux:


尝试:

newfd = os.open(''foobar'',os.O_EXCL | os.O_CREAT)

new_file = os.fdopen(newdf)

除了OSError,x:

如果x [1] ==''文件存在'':

handle_file_exists()


O_EXCL被打破NFS文件

系统,依赖它来执行锁定任务的程序

将包含竞争条件。使用lockfile执行原子

文件锁定的解决方案是在

相同的fs上创建一个唯一的文件(例如,合并主机名和pid),使用link(2)使

a链接到lockfile。如果link()返回0,则锁定为

成功。*

否则,在唯一文件上使用stat(2)来检查它是否为

链接数已增加到2,在这种情况下锁定也是

suc * cessful。]

You could be more "pythonic", and simply try to create the file,
catching the exception if if fails. This works on linux:

try:
newfd = os.open(''foobar'', os.O_EXCL | os.O_CREAT)
new_file = os.fdopen(newdf)
except OSError, x:
if x[1] == ''File exists'':
handle_file_exists()

[but beware unreliable on an NFS file system, from "man open":
O_EXCL is broken on NFS file
systems, programs which rely on it for performing locking tasks
will contain a race condition. The solution for performing atomic
file locking using a lockfile is to create a unique file on the
same fs (e.g., incorporating hostname and pid), use link(2) to make
a link to the lockfile. If link() returns 0, the lock is
successful.*
Otherwise, use stat(2) on the unique file to check if its
link count has increased to 2, in which case the lock is also
suc*cessful.]


这篇关于检查文件是否存在的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆