我不明白这个 "os.join" 函数是如何工作的?我不断收到错误,阅读 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

这是代码

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()

当我 运行 这样做时,它给我一个错误,如下所示:

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.'

我的笔记本电脑上什至没有这个“””'..\maildir/bailey-s/deleted_items/101.'"""目录路径,我试图通过替换目录中的'..'来更改路径按我保存所有文件的文件夹的实际路径名编码,没有任何变化。

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

这段代码是机器学习在线课程的一部分,我已经在这一点上停留了 3 个小时了。任何帮助将非常感激。

(P.S。这不是家庭作业问题,也没有附加成绩,这是一门免费在线课程)

您的测试数据不存在,因此无法找到。你应该 运行 再次启动代码并确保必要的 maildir 都在那里。

转到您的 udacity 项目目录中的工具,然后 运行 startup.py。 它大约有 400 Mb,所以请高枕无忧!

我知道这太晚了,但我在遇到完全相同的问题后发现了这个 post。

我在这里和其他网站上找到的所有答案,甚至是原始 github 中的问题请求,都只是“运行 startup.py”,我已经做到了。但是,它告诉我:

Traceback (most recent call last):

  File "K:\documents\Udacity\Mini-Projects\ud120-projects\text_learning\vectorize_text.py", line 48, in <module>
    email = open(path, "r")

FileNotFoundError: [Errno 2] No such file or directory: '..\maildir/bailey-s/deleted_items/101.'

就像你的一样。然后我找到了这个文件所在的位置,它确实在我的电脑上

我将 'tools' 添加到 os.path.join() 行,您可以在此处看到:

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])  <---original
            path = os.path.join('..','tools', path[:-1])
            print(path)
            email = open(path, "r")

这终于对我有用了。所以,我希望它能帮助以后遇到这个问题的任何人。

此外,我注意到我在课程的其他回购协议中发现的一些例子。他们的 'tools' 文件夹被命名为 'utils'.

Here is an example, this is a repo that someone tweaked to use jupyter notebooks to run the lessons 所以,使用你已有的那个。

  • 在您的 Udacity 课程文件夹中,首先转到 tools 目录,检查您是否有 maildir 文件夹,如果它有子文件夹,如果它们存在则返回text_learning/vectorize_text.py,找到这行代码path = os.path.join('..', path[:-1]),改成path = os.path.join('../tools/', path[:-1])

  • 在终端上,cd text_learning,然后 python vectorize_text.py,这应该可以解决问题。

  • 如果这不能解决问题,那么转到您的 udacity 项目目录中的工具和 运行 startup.py。等到过程完成

  • 重复步骤 1。