1

in my project I'm doing, I've always used this part of the script to check if a file existed or not:

path = os.path.join("myfile.txt")
conf = Path(path)
try:
    lc = conf.resolve()
except FileNotFoundError:
    print("new")
else:
    print("load")

It always worked (I used Python 3.4). Now, for some reason, I wanted to change the interpreter and use Python 3.7.1

Now, I only get printed "load", regardless of whether the file exists or not. How should I solve?

BlackFenix06
  • 535
  • 1
  • 5
  • 17

1 Answers1

5

The behavior of Path.resolve() changed in Python 3.6; you now need to pass strict=True to make it fail when the file doesn't exist.

But it's probably easier to use Path.exists() instead. (Or os.path.exists() if you want to work with path strings rather than Path objects.)

David Z
  • 116,302
  • 26
  • 230
  • 268
  • +1, using an failure / exception for a regular feature is (checking if a file exists) is always a bad idea. – xinbenlv Oct 30 '18 at 22:42
  • More than anything else, I needed a safe way to determine if a file existed or not. This is because I used it mainly to load or create a configuration file at the beginning of the script. I had found this method, so I did not know about this change in Python 3.6 – BlackFenix06 Oct 30 '18 at 22:42
  • The file verification was normally found in `__init__` in a class that took care of the configuration file. – BlackFenix06 Oct 30 '18 at 22:44
  • @BlackFenix06 why not just try always opening the file for reading and create it if you get a `FileNotFoundError` exception during that? – Sam Mason Oct 30 '18 at 22:56
  • @ZainanVictorZhou Well, Python is less particular about that than other languages (in particular contrast to C++ where throwing an exception entails significant overhead). But I do agree that ways of doing things that don't involve throwing an exception are usually preferable. – David Z Oct 30 '18 at 23:01
  • @David Z Sorry for the delay in the answer. The two options you suggested are good for both Windows, Mac and Linux? Or are there incompatibility problems? – BlackFenix06 Oct 31 '18 at 22:55
  • @BlackFenix06 What sort of incompatibility problems do you have in mind? Any particular reason you bring up that possibility? – David Z Nov 01 '18 at 01:54
  • @David Z I ask because I know that some instructions or functions maybe work only on one platform, or they are incompatible on another. Since I only have Windows, I can not test the script on Linux and Mac. Wanting to make the script as compatible as possible, I have to rely on the information given. That's why the question. – BlackFenix06 Nov 01 '18 at 09:37
  • @BlackFenix06 Ah, well I was a little surprised you asked because it would be very strange for commonly used functions like these to have significant platform differences not listed in the documentation. I can only personally attest that `os.path.exists()` works on Linux, but I believe all the functions I mentioned should work on all platforms. – David Z Nov 02 '18 at 00:27
  • Thanks for your help – BlackFenix06 Nov 02 '18 at 23:45