Tags: currentdirectory, exists, file, isfile, morethan, path, programming, python

best way to check if a file exists?

On Programmer » Python

7,700 words with 9 Comments; publish: Sat, 10 May 2008 02:05:00 GMT; (200140.63, « »)

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.

All Comments

Leave a comment...

  • 9 Comments
    • John Salerno wrote:

      > 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()?

      #1; Sat, 10 May 2008 02:06:00 GMT
    • At Tuesday 31/10/2006 18:01, John Salerno wrote:

      >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.

      Remember that things may change between you check the name and you

      actually create the file.

      Gabriel Genellina

      Softlab SRL

      ________________________________________

      __________

      Correo Yahoo!

      Espacio para todos tus mensajes, antivirus y antispam gratis!

      Abr tu cuenta ya! - http://correo.yahoo.com.ar

      #2; Sat, 10 May 2008 02:08:00 GMT
    • If you want to open the file for writing just open it

      with append mode. open(¨filename", "a"). If it

      doesn't exist - it'll create it, and if it does it'll

      start appending to the file

      For reading - os.path.exists("filename"). Or (doesn't

      make much sense, but still) try to open it for reading

      and python will throw an IOException which you can

      catch and handle.

      Michael

      -- John Salerno <johnjsal.python.todaysummary.com.NOSPAMgmail.com> wrote:

      > 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.

      > --

      > http://mail.python.org/mailman/listinfo/python-list

      >

      #3; Sat, 10 May 2008 02:08:00 GMT
    • On Oct 31, 4:01 pm, John Salerno <johnj....python.todaysummary.com.NOSPAMgmail.com> wrote:

      > 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.

      You could be more "pythonic", and simply try to create the file,

      catching the exception if if fails. This works on linux:

      try:

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

      new_file =3D os.fdopen(newdf)

      except OSError, x:

      if x[1] =3D=3D '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.=AD

      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=ADcessful.]

      #4; Sat, 10 May 2008 02:10:00 GMT
    • georgeryoung.python.todaysummary.com.gmail.com writes:

      > On Oct 31, 4:01 pm, John Salerno <johnj....python.todaysummary.com.NOSPAMgmail.com> wrote:

      I see that 'os.path.exists' has already been pointed out in a later

      post.

      > 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()

      Are you sure that both of 'os.open' *and* 'os.fdopen' will only ever

      raise OSError if the file already exists, and not for any other OS

      errors?

      \ "We have to go forth and crush every world view that doesn't |

      `\ believe in tolerance and free speech." -- David Brin |

      _o__) |

      Ben Finney

      #5; Sat, 10 May 2008 02:10:00 GMT
    • On Tuesday 31 October 2006 16:01, John Salerno

      wrote:

      > 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.

      How about something like one of these;

      if os.path.isfile(vfileName) not True :

      male file

      or

      if os.path.isfile (os.path.join(os.getcwd(),

      vFileName) )==True :

      do something

      jim-on-linux

      http://www.inqvista.com

      #6; Sat, 10 May 2008 02:11:00 GMT
    • John Salerno wrote:

      > 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.

      You could try to read the file, if that fails it doesn't exist:

      try:

      f = open('xxx')

      except IOError:

      f = open('xxx', 'w')

      print "file doesn't exist"

      print f

      #7; Sat, 10 May 2008 02:13:00 GMT
    • "wittempj.python.todaysummary.com.hotmail.com" <martin.witte.python.todaysummary.com.gmail.com> writes:

      > You could try to read the file, if that fails it doesn't exist:

      > try:

      > f = open('xxx')

      > except IOError:

      > f = open('xxx', 'w')

      > print "file doesn't exist"

      > print f

      Except that there are other conditions than "File doesn't exist" that

      can cause an 'open' to fail.

      \ "If you continue running Windows, your system may become |

      `\ unstable." -- Microsoft, Windows 95 BSOD message |

      _o__) |

      Ben Finney

      #8; Sat, 10 May 2008 02:14:00 GMT
    • Ben Finney wrote:

      > "wittempj.python.todaysummary.com.hotmail.com" <martin.witte.python.todaysummary.com.gmail.com> writes:

      >

      > Except that there are other conditions than "File doesn't exist" that

      > can cause an 'open' to fail.

      >

      Ok, true. You can test explicit on non existence as follows, and then

      decide to open the file

      import errno

      try:

      f = open('xxx')

      except IOError, e:

      if e.errno == errno.ENOENT:

      f = open('xxx', 'w')

      print f

      > --

      > \ "If you continue running Windows, your system may become |

      > `\ unstable." -- Microsoft, Windows 95 BSOD message |

      > _o__) |

      > Ben Finney

      #9; Sat, 10 May 2008 02:15:00 GMT