1

According to all the sources I've read, the open method creates a file or overwrites one with an existing name. However I am trying to use it and i get an error:

File not found - newlist.txt (Access is denied)
I/O operation failed.

I tried to read a file, and couldn't. Are you sure that file exists? If it does exist, did you specify the correct directory/folder?

def getIngredients(path, basename):
  ingredient = []
  filename = path + '\\' + basename
  file = open(filename, "r")
  for item in file: 
    if item.find("name") > -1:
      startindex = item.find("name") + 5
      endindex = item.find("<//name>") - 7
      ingredients = item[startindex:endindex]
      ingredient.append(ingredients)

  del ingredient[0]
  del ingredient[4]


  for item in ingredient:
    printNow(item)

  file2 = open('newlist.txt', 'w+')  

  for item in ingredient:  
     file2.write("%s \n" % item) 

As you can see i'm trying to write the list i've made into a file, but its not creating it like it should. I've tried all the different modes for the open function and they all give me the same error.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user1390754
  • 171
  • 1
  • 1
  • 10
  • Are you on a posix environment? the \\ slash is odd... not sure what you are doing there. It should probably just be "/" right? – Steve Ross May 13 '12 at 01:33
  • this is on JES(Jython Environment for Students) forgot to mention that may be why.the double slash \\ part of the code was provided by the lecturer so not quite sure about that. its the second open function that isnt working for me also – user1390754 May 13 '12 at 01:36
  • [This might help.](http://www.penzilla.net/tutorials/python/fileio/) or [here](http://stackoverflow.com/questions/1466000/python-open-built-in-function-difference-between-modes-a-a-w-w-and-r). Also, I'd be curious if you do have write access to the location the Python file is executing. Maybe you could try an absolute path for the newlist.txt open to make sure? – inevio May 13 '12 at 01:38
  • Got it to work, the absolute path idea worked, thanks matthew! – user1390754 May 13 '12 at 01:54
  • @user1390754: I would suggest that you have this code reviewed on codereview.stackexchange.com. This would really help you write more efficient, simpler and more Pythonic code. :) – Eric O. Lebigot May 13 '12 at 03:50

4 Answers4

1

It looks like you do not have write access to the current working directory. You can get the Python working directory with import os; print os.getcwd().

You should then check whether you have write access in this directory. This can be done in Python with

import os
cwd = os.getcwd()
print "Write access granted to current directory", cwd, '>', os.access(cwd, os.W_OK)

If you get False (no write access), then you must put your newfile.txt file somewhere else (maybe at path + '/newfile.txt'?).

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
0

Are you certain the directory that you're trying to create the folder in exists?

If it does NOT... Then the OS won't be able to create the file.

poy
  • 10,063
  • 9
  • 49
  • 74
  • possibly, but how do i specify the location? theres no parameter in the open function that you can put to tell it where to make the file i think.. – user1390754 May 13 '12 at 01:45
  • You can directly put the filename. For example open("D:/Code/test.txt",'r') – user907629 May 13 '12 at 02:16
0

This looks like a permissions problem.

either the directory does not exist or your user doesn't have the permissions to write into this directory .

Jay D
  • 3,263
  • 4
  • 32
  • 48
  • yes i'm thinking that as well. do you by any chance know how to grant permission? i'm not quite sure what directory it is writing into, but by default i believe it writes into the JES directory. – user1390754 May 13 '12 at 01:50
  • you can use `chmod +777 'folder name'` . This will make the folder readable + writtable + executable – Jay D May 13 '12 at 01:52
  • @user1390754: Note that it might be forbidden to do `chmod +777`, and that it is generally dangerous to do this mode change anyway, as it grants write (including erase) permission to *other* users. – Eric O. Lebigot May 13 '12 at 02:00
  • @EOL I disagree if your user does not have ownership of parent folder , you can't even do chmod. To give an example if `user2` does not own Or does not even have read access to `/home/user1` then `user2` can't do `chmod` to `/home/user2/partyplace`. Thus even though `user2` does a `chmod 777 ` to `partyplace` it's still fine. Assuming `user2` owns `/home/user2` – Jay D May 13 '12 at 03:25
  • @JayD: We fully agree on the fact that "if your user does not have ownership of parent folder, you can't even do chmod": that's what I meant by "it might be forbidden to do `chmod +777`". However, you last statement is incorrect: if `/home/user2/partyplace` has `777` access rights, then *anybody* can do anything with its contents, including deleting it (in fact, access rights to `/home/user2/` are irrelevant). This is not a recommended situation. – Eric O. Lebigot May 13 '12 at 03:45
0

I guess the possible problems may be:

1) You are passing the path and basename as parameters. If you are passing the parameters as strings, then you may get this problem:

For example:

def getIngredients(path, basename):
  ingredient = []
  filename = path + '\\' + basename


getIngredients("D","newlist.txt")

If you passing the parameters the above way, this means you are doing this

filename = "D" + "\\" + "newlist.txt"

2) You did not include a colon(:) after the path + in the filename.

3) Maybe, the file does not exist.

daedalus
  • 10,873
  • 5
  • 50
  • 71
user907629
  • 544
  • 1
  • 7
  • 17