Python Forum

Full Version: myfile = open(datafile, 'rb') not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Morning All,
i'm trying to write code to use requests.post() but it keeps skipping the try and printing 'Error' with one particular line.

try:
    url = http:\\11.11.11.11    # url changed for security
    print(url)
    myFile = open(<datafile>, 'rb')    # This is the line i'm having issues with
    files = {'file': myFile}
    #print(files)
    r = requests.post(url, file=files)
except:
    print('Error')
any help would be greatly appreciated
Many Thanks
It should, if your code contains this row:

url = http:\\11.11.11.11     # invalid syntax, must be string
sorry, my bad, it is a string in the code, I just changed the address and forgot to put "" around it
Don't use general except clause that will mask what the error is. Remove the try/except to get exception description
After taking away the try/except I found out why it wasn't liking it. The path was not recognized.
I retyped the path and it worked, only problem is that after double checking the previous path was actually correct with no errors but for some reason retyping it worked.
Confused yes, but it's working :)