1

How can I python to open a file on my mac? I am currently working on a project to proof read for curse words but I can't seem to be able to open the word document in which I want python to check. I have all the code done and dusted but I am having trouble opening the file for python to read. I am also a beginner so if you could please explain it to me that would be great.

Here is a sample of my code:

def read_text():
    quotes = open("/Users/AquaAurelius/Documents/Home/Programming/Houston")
    contents_of_file = quotes.read()
    #print contents of file
    print(contents_of_file)
    quotes.close()

read_text()
5
  • 1
    What is the error you are getting? Commented May 30, 2016 at 7:50
  • "open the word document" - has the file been created using Microsoft Word? Is the file called Houston, or is that the directory name? Commented May 30, 2016 at 8:08
  • the word document was created using Microsoft Word and I named is Houston. Commented May 30, 2016 at 9:17
  • I'm getting the error: Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> open("/Users/AquaAurelius/Documents/Home/Programming/Houston.txt") FileNotFoundError: [Errno 2] No such file or directory: '/Users/AquaAurelius/Documents/Home/Programming/Houston.txt' Commented May 30, 2016 at 18:53
  • It's not possible to answer this question properly without more information. It looks like your filename has an extension, but your system hides filename extensions. As Cam_Aust explains in his answer you need to make those extensions visible. If this is a plain text file then you can easily read the words from it with Python, but if it's one of the many other formats that Word can save documents in then the task will be more difficult. You will still be able to open it, but extracting the actual text may be harder to do, especially if you've used an old proprietary .doc format. Commented Jun 1, 2016 at 22:18

2 Answers 2

4

Maybe you are currently open a folder with

quotes = open("/Users/AquaAurelius/Documents/Home/Programming/Houston")

You should open the file:

quotes = open("/Users/AquaAurelius/Documents/Home/Programming/Houston/YOUR_FILENAME.txt")

See the missing part at the end?

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

1 Comment

Salomonderossi adding .txt at the end doesn't work. I am using a mac and used Microsoft word to create the document. What file has the file extension 1.txt because I might be able to use that. Thanks
2

Beginner noted. Understand that I am new to Python also.

First I think in the above solutions the assumption is that your Word doc file is in a folder Houston. From my reading I understand your file name is Houston, and the folder it is in is called Programming.

Secondly if the file you are seeking to open is a Word doc file, the correct file name needs to include the correct file type extension. If should be either .doc or .docx, unless when you saved the file in Word deliberately to another type of file (eg .rtf, .txt, .pdf)

Depending on your OSX Finder preference settings, file extensions may not be visible to you. If they are not visible, click once on your target file in OSX to select it, then press Command and the letter "I" key (together) for the information of this file. In the information box that opens, look at the top to see the name of the file (Houston) and the file extension of your file.

Putting these two considerations together, I suggest try the following - refining salomonderossi solution.

If the full file name with .extension is Houston.doc, then try:

open("/Users/AquaAurelius/Documents/Home/Programming/Houston.doc")

If it is Houston.docx, then try:

open("/Users/AquaAurelius/Documents/Home/Programming/Houston.docx")

If this works, you will likely find there is a lot more characters in the file when it is opened in Python than what you see when the same file is opened in Word. The extra characters are hidden characters that Word as a program uses to format and manage your doc file in Word. However, all the text that is of interest to you should still be there to check for curse words.

If you want to then save this file and have others open it in Word, you need to be very careful you only change words that are visible text in Word, not the additional text you see in Python.

I have since tried this solution with a .docx and simple text file and neither have worked for me.

From a web search it seems to achieve your goal you may need to install an additional python package python-docx. See python-docx home page link

1 Comment

I have updated more information to my answer. If this works, that would be good to know.

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.