Python - create next file
Question: I am writing a small script. The script creates `.txt` files. I do not want to
replace existing files. So what I want python to do is to check if the file
already exists. If it does not it can proceed. If the file does exists I would
like python to increment the name and than check again if the file already
exists. If the file does not already exist python may create it. EXAMPLE:
current dir has these files in it:

`file_001.txt`

`file_002.txt`

I want python to see that the two files exists and make the next file:

`file_003.txt`

creating files can be done like this:

    
    
    f = open("file_001.txt", "w")
    f.write('something')
    f.close()
    

[checking](http://stackoverflow.com/questions/82831/how-do-i-check-if-a-file-
exists-using-python) if a file exists:

    
    
    import os.path
    os.path.isfile(fname)
    


Answer: If you want to check whether it's both a file and that it exist then use
`os.path.exists` along with `os.path.isfile`. Or else just the former seems
suffice. Following might help:

    
    
    import os.path as op
    print op.exists(fname) and op.isfile(fname)
    

or just `print op.exists(fname)`

