2

I've tried to open a file that does exist but I keep getting an error message of:

No such file or directory on Mac

I have made sure it is in the same directory as the python code. I've also tried changing the name of the excel doc and whether the doc is .xls and .xlsx but it has not worked.

Here are several versions I've tried, but so far they all give me the same result.

import os.path
book = open(os.path.expanduser("~/Desktop/Crimes.xlsx"))

or

import xlrd
import os.path
book = xlrd.open_workbook(os.path.join("/Users/caitlinwesterfield",'Crime.xls')

or

import xlrd
book = xlrd.open_workbook('Crime.xls')

or

import xlrd
book = open('/Users/caitlinwesterfield/Desktop/Crime.xls', "r")

or

import xlrd
book = open("~/Crime.xls", "r")

or

import xlrd
book = open(os.path.expanduser(r"~/Desktop/Crime.xls"))

or

import xlrd
book = open('Crime.xls')

or

import xlrd
book = open(os.path.expanduser("/Users/caitlinwesterfield/Desktop/Crime.xls"))

or

import xlrd
import os
book = xlrd.open_workbook(os.path.join("/Users/caitlinwesterfield","TypesOfCrime.xls"))

or

import xlrd
book = xlrd.open_workbook("typesofcrime.xls")
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
CaitlinM
  • 21
  • 1
  • 6
  • Did you try open a text file instead of `.xls` file? – Remi Guan Nov 08 '15 at 01:17
  • It still could not find the document as a .txt file, but I'm not sure how this would allow me to iterate through rows later in the excel doc – CaitlinM Nov 08 '15 at 02:50
  • So as you said, it could not find **any** file. Can you write some text into a file? – Remi Guan Nov 08 '15 at 02:52
  • The issue is that the code could find the file and find the path, it just could not find the document in xlrd.open_workbook or open . I'm not sure what that means – CaitlinM Nov 08 '15 at 02:55
  • For example, this code works: print os.path.abspath('Types_Of_Crime.xlsx') – CaitlinM Nov 08 '15 at 02:56
  • Well, so Python could find the file, but when you're trying to open it it raise `No such file or directory`? That's impossible! – Remi Guan Nov 08 '15 at 02:57
  • Yes but that is what's happening! – CaitlinM Nov 08 '15 at 02:58
  • When I used this: book = xlrd.open_workbook(os.path.join(os.path.abspath('Types_Of_Crime.xlsx')) The result is [Errno 2] No such file or directory: '/Users/caitlinwesterfield/Types_Of_Crime.xlsx' meaning it found the path, but did not open the file – CaitlinM Nov 08 '15 at 03:01
  • Wait, I'm trying to open a doesn't exist file, it raise `FileNotFoundError: [Errno 2] No such...`, but you got an `IOError`? – Remi Guan Nov 08 '15 at 03:02
  • Yes I got an IOError – CaitlinM Nov 08 '15 at 03:04
  • I found [another](http://stackoverflow.com/questions/13000455/error-in-python-ioerror-errno-2-no-such-file-or-directory-data-csv) question, but seems like you've tried all of the solutions...What's that file looks like? – Remi Guan Nov 08 '15 at 03:07
  • I tried the code on multiple different documents and it seems that I cannot open any files to read using code of : book = open(os.path.abspath('text.docx') What do you suggest? I am using canopy and have imported the os package (which is working as the error is unable to find '/Users/caitlinwesterfield/text.docx' ) – CaitlinM Nov 08 '15 at 03:16
  • Well, which version of Python are you using? I think this problem is about Python... – Remi Guan Nov 08 '15 at 03:18
  • Are you on an HFS+ OS/X Volume that is case sensitive? If you are, have you verified that the full path and file name matches the proper case? – Michael Petch Nov 08 '15 at 03:18
  • I think so Michael? I directly copied and pasted both the full path and file name. And Kevin I think Python 2.1? I think it says I am in IPython , but I'm not sure what this means – CaitlinM Nov 08 '15 at 03:26
  • You said that "The issue is that the code could find the file and find the path" but I'm not sure that's the case. The mere fact that you can do os.path.abspath on the path doesn't show that the path is visible to python: abspath just manipulates paths as strings and doesn't care whether the file/path in question really exists (try it with a missing file!). What happens if you do os.path.realpath instead? – circular-ruin Nov 08 '15 at 03:31
  • Using realpath still said it No such file or directory – CaitlinM Nov 08 '15 at 03:38
  • OK. So that makes me think that it really can't find the file or the path. Can you try os.listdir("/Users/caitlinwesterfield"). If that fails, can you try os.listdir("/Users") and if that fails what about os.listdir("/") ? – circular-ruin Nov 08 '15 at 03:54
  • PS if you reply to someone else's comment, it's a good idea to start your answer with that user's handle (e.g. @CaitlinM or @circular-ruin). That way the user will be notified about the reply... – circular-ruin Nov 08 '15 at 03:57
  • Ok @circular-ruin after trying os.listdir("/Users/caitlinwesterfield") the error I got was ----> 8 book = open(c) TypeError: coercing to Unicode: need string or buffer, list found – CaitlinM Nov 08 '15 at 04:10
  • The same occurred for os.listdir("/") – CaitlinM Nov 08 '15 at 04:11
  • Also I'm a bit confused on how opening to these areas would allow me to find my document? I'm sorry I'm such a nuisance. I'm (clearly) very new to python – CaitlinM Nov 08 '15 at 04:13
  • Sorry, I am confused by your last answer. Are you saying that when you tried typing os.listdir("/") at the python prompt, python responded with an exception "TypeError: coercing to Unicode: need string or buffer, list found"? – circular-ruin Nov 08 '15 at 04:15
  • I'm not suggesting that you open those areas directly... in fact if you did open(os.listdir("/")) that wouldn't work at all! (And indeed that would explain the error you reported just above.) The idea is that if you try `print(os.listdir("/") )` it will either print the contents of /, or will give an error message. If it works, then you can try `print(os.listdir("/Users") )` and so on. You kind of work up to `Users/caitlinwesterfield/Desktop/Crime.xls` and see at what point the problem hits.... – circular-ruin Nov 08 '15 at 04:18
  • @circular-ruin thank you that made a lot more sense. I worked my way all the way up to Desktop, but once I added Crime.xlsx I received this error: OSError: [Errno 20] Not a directory: '/Users/caitlinwesterfield/Desktop/Crime.xlsx' Does this mean it cannot find Crime at all? When I put Crime in quotations it did not work either – CaitlinM Nov 08 '15 at 04:24
  • @CaitlinM : Is it Crime or Crimes ? You have used both in your question. Maybe you just have the name wrong? And is it Crimes.xlsx or Crime.xlsx. I think you need to verify **exactly** what the file name is. – Michael Petch Nov 08 '15 at 04:28
  • OK. You wouldn't expect to be able to `os.listdir` a file like `Users/caitlinwesterfield/Desktop/Crime.xls`, so the error you got in the end is not surprising. So just to be clear, `print(os.listdir('/Users/caitlinwesterfield/Desktop/'))` gives some output (no error message), but `print(open('/Users/caitlinwesterfield/Desktop/Crime.xlsx').readlines())` gives `No such file or directory`? – circular-ruin Nov 08 '15 at 04:28
  • Wait that worked!!!! Thank you!!!! – CaitlinM Nov 08 '15 at 04:45
  • @CaitlinM : what was the problem? – Michael Petch Nov 08 '15 at 04:45
  • I'm not sure. But when I copied and pasted in open('/Users/caitlinwesterfield/Desktop/Crime.xlsx') it worked – CaitlinM Nov 08 '15 at 04:53
  • @CaitlynM: You are aware that if you go through your question, you actually never tried loading that particular file name. You either left off the trailing `x` in xlsx, you spelled Crime with an `s` in some of them and in others you didn't use the directory `Desktop`. My opinion is that you have been using the wrong file and/or path name all along. – Michael Petch Nov 08 '15 at 04:56
  • Now that we know the exact file name you were trying to use, my guess is that this works `book = open(os.path.expanduser("~/Desktop/Crime.xlsx"))` This is the same as your very first attempt in your question except I have removed the `s` from Crime. Does that work? – Michael Petch Nov 08 '15 at 05:20

2 Answers2

0

Have you tried checking if there was a permissions issue?

Open up your terminal and cd to the right directory

cd -l

Here is what those permissions mean:

rwxrwxrwx  <-- r refers to read, w to write, and x to execute
---------  <-- a hyphen refers to the permission not existing 

enter image description here

Try running:

chmod 777 Crimes.xlsx
# you might need to add sudo if you get permission error:
sudo chmod 777 Crimes.xlsx

image source and more info

how to use the terminal

devoxel
  • 76
  • 1
  • 5
0

Verify that the file is NOT corrupt, e.g. total file size is correct.

Download the file again if it is from online.

This set of ACTIONS solved this problem in my case:

(Error 2 - No such file or directory.)

I hope this helps you too.

masarapmabuhay
  • 466
  • 1
  • 9
  • 15