0

This is the code:

def edit(aFile):
   s = ''
   filename = getMediaPath() + aFile
   inputfile = open(filename, 'r')
   read = inputfile.readlines()
   inputfile.close()
   for lines in read:
     lines = lines.lower()
     lines = lines.replace("it's", "this is")
     lines = lines.capitalize()
     s = s + str(lines)
   newfile = getMediaPath() + 'happyEdited.txt'
   x = open(newfile, 'w')
   x.write(s)
   x.close()

The error I get is on the "inputfile = " line. It says: "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?"**

I've tried entering aFile as a string with the media path. I've tried setting aFile equal to it's media path but nothing works. When I take the parameter out and replace aFile in the code with the name of the .txt file the code works.

Thank y'all!

falsetru
  • 357,413
  • 63
  • 732
  • 636

3 Answers3

1

A few suggestions:

You could include a checking routine for debugging, e.g.,

import os
print os.path.exists(filename) 
print os.path.isfile(filename)

And also, I would recommend to use

with open(filename,'r') as inputfile:
    # do your stuff

instead of

inputfile = open(filename, 'r')
# do your stuff
inputfile.close()

Because with makes sure that the file stream will be definitely closed if a problem occurs in the # do your stuff section, otherwise you have to use excepts to ensure it, which is just a little bit more effort. with is just a more convenient way.

And I think what you need to get your case to work could be:

newfile = getMediaPath() + '/happyEdited.txt'
0

I am just adding kwatford`s comment as answer in here. What you need to change is

filename = os.path.join(getMediaPath(),aFile)

newfile = os.path.join(getMediaPath() , 'happyEdited.txt')
Rahul
  • 11,129
  • 17
  • 63
  • 76
0

The main problem here is probably that you are using simple strings that represent relative file paths. If you were to provide a full traceback, then I could give you a better answer.

Now, this will give you problems a lot of the times, and so it is best practice to always use absolute paths.

Now, what is an absolute path, you say? Well, its the path that goes all the way from your drive to your actual file destination. For example: C:/Foo/Bar/Cheese/happy.py. A relative file path is a path relative to your current directory. For example you were in your command line and you were @ C:/Foo/Bar/Cheese/happy.py, and if there was another file in the same directory, say more.py, then you could reference it as ./more.py, but this can lead to several problems as you are facing right now.

So, what is the solution? Like I said, use absolute paths, now how do you do this? Well you use a module called os.

So, something like this:

import os

file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "name_of_file_in_current_dir")).replace(os.pardir, "/")

Now let me tell you what this means, os.path.abspath gives you an absolute path. os.path.join allows you to join paths in a flexible ways, you can join folders. os.path.dirname gives you the absolute path a specified file, in this case __file__. __file__ is a special variable. Finally, internally an OS can use different ways to separate directories, some use //, some use \, some use \\. Now, this is what works best /, since it works on all systems. We use os.pardir because it will work on all systems, Windows, Linux and Unix, which makes your code portable! :D

Also, a good recommendation would be using the with statement. Like so:

with open(file_path) as file:

This is the same as putting a try/catch block around it, but in once simple line. It also opens and closes the file stream for you.

Community
  • 1
  • 1
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
  • No, there is nothing inherently wrong with relative paths. It just means that the path is relative to your current directory, not your file system root. Although you should convert to absolute paths if you think your current working directory will change (e.g., you are writing to a config file that is consumed by some other service), they are perfectly fine to use day to day. – tdelaney Jul 26 '13 at 15:40
  • @tdelaney: Actually, I had a similar problem like this when I was trying to load a `config.JSON` file. I used a relative path, but since I was in my IDE, and my CMD was not in my working dir, I was having problems. Using absolute paths solve a lot of problems, and makes code a lot more portable. Ofcourse, there is nothing _inherently_ wrong, but its good practice to use absolute paths. – Games Brainiac Jul 26 '13 at 15:42