0

I have been working on a web scraping program and it has recently just stopped working and is giving me the following error.

Traceback (most recent call last):
  File "C:/Users/Bob/Desktop/test 3.5.py", line 7, in <module>
    with open(saveDB,'rb') as f:
TypeError: invalid file: WindowsPath('Z:/project1/MasterWellDB.txt')

Where Z: is a network drive, but I have also moved the file locally and the error still exists.

I have tried multiple python versions, uninstalled and reinstalled visual studio multiple times and I am still clueless.

Here is my code:

from pathlib import Path
import pickle
# Opening well database
saveDB = Path(r"Z:\project1\MasterWellDB.txt")
#open picked DB if avaiable else remake database
if saveDB.exists():
    with open(saveDB,'rb') as f:
        wells = pickle.load(f)
        print('success!')
        

Any help would be greatly appreciated. Thanks!

MrClean
  • 1,300
  • 2
  • 12
  • 17
  • Not sure this is the reason but you're using both `/` and `\ ` separators for you're path. If you're on windows use `\ ` else use `/`. – Ulysse BN Jul 11 '17 at 16:58
  • The top code is there error that is automatically generated, I didn't write that code. I am using only \ in my code, I just tried using / and that did not change anything. – MrClean Jul 11 '17 at 17:07
  • and why do you use the `r` prefix to your string ? (It usually stands for regex) – Ulysse BN Jul 11 '17 at 17:09
  • https://stackoverflow.com/questions/19034822/unknown-python-expression-filename-r-path-to-file – MrClean Jul 11 '17 at 17:11
  • Oh thanks! Then I don't have any other clue :/ – Ulysse BN Jul 11 '17 at 17:12
  • Don't call close on f. That's what the with statement is all about.... – jlaur Jul 12 '17 at 11:48
  • Btw the path in the stacktrace is not same path as the one in the code you uploaded here – jlaur Jul 12 '17 at 11:50
  • Does this answer your question? [When using pathlib, getting error: TypeError: invalid file: PosixPath('example.txt')](https://stackoverflow.com/questions/42694112/when-using-pathlib-getting-error-typeerror-invalid-file-posixpathexample-t) – Alex Mar 30 '20 at 17:33

0 Answers0