1

I want to open the file as output.

It turned out "exit"

However, I want to read the file or write the file. Through the test, it seems not the IOError. How to open the file?

I tried several codes and still could not find the way to open it.

try:
    my_file_handle=open("/Users/name/Desktop/Trip.docx")
except IOError:
    print("File not found or path is incorrect")
finally:
    print("exit")

Then, I changed "docx" to "doc" and add 'r' mode and call it.

I tried:

    try:
        my_file_handle=open('/Users/name/Desktop/Trip.doc','r')
        my_file_handle.read()
        print("hi")
    except IOError:
        print("File not found or path is incorrect")
    finally:
        print("exit")

it turned out "exit" and my_file_handle.read()

File"/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/codecs.py", line 321, in decode (result, consumed) = self._buffer_decode(data, self.errors, final)

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte

8
  • What’s your error message? I believe when opening a file you need to specify whether you’re reading, writing, etc. Commented May 20, 2018 at 9:10
  • I want to read it. Commented May 20, 2018 at 9:16
  • What are you planning to do with the open file? docx files are zip files so you should open them in binary mode (mode='rb'). Also you can open them with zipfile.ZipFile('/Users/name/Desktop/Trip.docx') and access the archived files. Commented May 20, 2018 at 9:56
  • I tried "rb". It could not work. Commented May 20, 2018 at 10:02
  • Again, what do you want to do with the file once opened? Perhaps a library like python-docx would be more useful. Commented May 20, 2018 at 10:06

2 Answers 2

2

Have you tested if the file is actually open at the end of the block? Because I think you'll find it is. The code in the 'finally' part of a try: except: block is obeyed regardless of whether there is an exception or not.

From the python documentation:

If finally is present, it specifies a ‘cleanup’ handler. The try clause is executed, including any except and else clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. If there is a saved exception it is re-raised at the end of the finally clause. If the finally clause raises another exception, the saved exception is set as the context of the new exception. If the finally clause executes a return or break statement, the saved exception is discarded:

Sign up to request clarification or add additional context in comments.

20 Comments

No, it still could not open the file.
Curious - and there is no traceback? Or other sign of an exception?
Actually, can you try this for me? "with open(/Users/name/Dexktop/Trip.docx" as f: <newline><indent>r=f.read(20)<newline>' where <..> are the actions within the angle brackets and see if r contains anything afterewards?
From my comment in reply to @Caligary above can you try specifying a second parameter to the open of 'rb'. The b is for binary. .docx file are in effect sipped .doc files.
try as f-->I do not know if my key-in wrong. It shows invalid syntax.
|
0

When opening the file, you have to specify your intensions. For writing that would be:

    my_file_handle=open("/Users/name/Desktop/Trip.docx","w")

2 Comments

No, you don't but if you don't r is implied. Actually, that gives me an idea... is this because .docx files are binary?
I tried another -->.pages . It still could not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.