Search code examples
python-2.7relative-pathpython-os

I don't understand how this "os.join" function is working? I am getting errors constantly and no reading on os functions is helping me


Here's the code

sys.path.append( "../tools/" )
from parse_out_email_text import parseOutText #(its just another .py file that has a function I wrote)

from_sara  = open("from_sara.txt", "r")
from_chris = open("from_chris.txt", "r")

from_data = []
word_data = []

temp_counter = 0

for name, from_person in [("sara", from_sara), ("chris", from_chris)]:
  for path in from_person:
    ### only look at first 200 emails when developing
    ### once everything is working, remove this line to run over full dataset
    temp_counter += 1
    if temp_counter < 200:
        path = os.path.join('..', path[:-1]) #(THIS IS THE PART I CAN'T GET MY HEAD AROUND)
        print path
        email = open(path, "r")

        email.close()

print "emails processed"
from_sara.close()
from_chris.close()

When I run this, it gives me an error as shown below:

Traceback (most recent call last):
..\maildir/bailey-s/deleted_items/101.
File "C:/Users/AmitSingh/Desktop/Data/Udacity/Naya_attempt/vectorize_text.py", line 47, in <module>
email = open(path, "r")
IOError: [Errno 2] No such file or directory: '..\\maildir/bailey-s/deleted_items/101.'

I don't even have this """'..\maildir/bailey-s/deleted_items/101.'""" directory path on my laptop, I tried to change the path by replacing the '..' in the code by the actual path name to the folder where I keep all the files, and nothing changes.

path = os.path.join('..', path[:-1])

This code is part of an online course on machine learning and I have been stuck at this point for 3 hours now. Any help would be really appreciated.

(P.S. This is not a homework question and there are no grades attached to this, its a free online course)


Solution

  • your test data is not there so it cannot find it. you should run start-up code again and make sure the necessary maildir are all there.