1

I work from two different computers with the same script. Writing files to my cloud storage \has a slightly different path on both computers. I thought I could use a try-except to simply see which path works (depending on which computer I am using).

I thought I could solve this by doing the following:

import os
try:
    os.chdir('C://Users//comp1//Cloud//Data')
except:
    os.chdir('C://Users//comp2//Cloud//Data')

However if I run it, I still get:

FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C://Users//comp1//Cloud//Data')

Could someone tell me what I am doing wrong here, or whether I should perhaps use a different solution.

Tom
  • 2,173
  • 1
  • 17
  • 44
  • 3
    Don't use double slashes, those are not a thing. Single `/` should work even on Windows, which normally uses backslash, thanks to Python doing the conversion for you. – Thomas Jun 03 '22 at 10:12
  • It's either two backslashes or one forward slash. – Jinter Jun 03 '22 at 10:12
  • When using different networks, typically a hostname test can be used to pre-assign the appropriate path, and forget the expensive try/except all together (in this case). – S3DEV Jun 03 '22 at 10:27
  • You should use the `r` modifier for path strings `r'C:\Users\...'` and construct them normally – Steven Summers Jun 03 '22 at 10:28
  • 1
    Thank you for all your comments guys. As note, I would like to say that the double forward slashes did/do work, even though they are apparently not correct (and I will stop using them). The issue was a very silly one. There was another rogue `os.chir` statement in my script that I had missed. The second time I ran the code it was that statement creating the error (because I was working on the wrong computer). – Tom Jun 03 '22 at 10:40
  • Glad to hear you figured it out. However I'd still recommend using if-else instead of try-except as I have mentioned [here](https://stackoverflow.com/a/72488239/13536496). – Mark Jun 03 '22 at 10:43

1 Answers1

2

As multiple comments have suggested, replace the double forward slashes with one forward slash, or use double backward slashes.

Now I ran your code and it does seem to work, but it depends on the context (is this your entire script?). However you could do better with a if-else statement rather than try-except because the latter is a lot slower first of all, and generally not the best practice.

import os

if os.path.isdir('C:/Users/comp1/Cloud/Data'):
    os.chdir('C:/Users/comp1/Cloud/Data')
elif os.path.isdir('C:/Users/comp2/Cloud/Data'):
    os.chdir('C:/Users/comp2/Cloud/Data')

If you'd like to stick to try-except and neither directories exist, you could try using nesting your try-except like this:

Edit: Forgot to mention, thanks @Tzane, do not use bare try-except statements.

import os

try:
    try:
        os.chdir('C:/Users/comp1/Cloud/Data')
    except FileNotFoundError:
        os.chdir('C:/Users/comp2/Cloud/Data')
except FileNotFoundError:
    # handle the case where neither directory exist
    pass

Or to combine the two suggestions:

import os

if os.path.isdir('C:/Users/comp1/Cloud/Data'):
    os.chdir('C:/Users/comp1/Cloud/Data')
elif os.path.isdir('C:/Users/comp2/Cloud/Data'):
    os.chdir('C:/Users/comp2/Cloud/Data')
else:
    # handle the case where neither directory exist
    pass
Mark
  • 214
  • 2
  • 13
  • 2
    [You shouldn't use bare except statements](https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except) – Tzane Jun 03 '22 at 10:33