代码之家  ›  专栏  ›  技术社区  ›  Sean

在Python中将目录传递给变量

  •  -1
  • Sean  · 技术社区  · 7 年前

    我正在尝试修改GitHub的一个脚本,该脚本将访问TAR文件并对其进行处理。代码中有一个变量需要指向文件所在的根目录(我认为…)。代码如下:

    def make_Dictionary(root_dir):
        emails_dirs = [os.path.join(root_dir,f) for f in os.listdir(root_dir)]    
        all_words = []       
        for emails_dir in emails_dirs:
            emails = [os.path.join(emails_dir,f) for f in os.listdir(emails_dir)]
            for mail in emails:
                with open(mail) as m:
                     for line in m:
                         words = line.split()
                         all_words += words
        dictionary = Counter(all_words)
        list_to_remove = dictionary.keys()
    
        for item in list_to_remove:
            if item.isalpha() == False: 
                del dictionary[item]
            elif len(item) == 1:
                 del dictionary[item]
        dictionary = dictionary.most_common(4000)
    
        np.save('dict_movie.npy',dictionary) 
    
        return dictionary
    
    root_dir = sys.path[0]
    dictionary = make_Dictionary(root_dir)
    

    root\u dir正在抛出:

      File "C:\Users\seand\eclipse-workspace\sentiment_project\src\root\nested\movie-polarity.py", line 22, in make_Dictionary
        emails = [os.path.join(emails_dir,f) for f in os.listdir(emails_dir)]
    NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\\Users\\seand\\eclipse-workspace\\sentiment_project\\src\\root\\nested\\movie-polarity-tfidf.py'
    

    方向状态“注意:需要相应地设置movie-polarity-tfidf.py和movie-polarity.py中语料库的目录路径。”但我指定的路径包含脚本所需的语料库TAR文件。我不明白为什么,如果脚本正在查找目录,那么这个.py文件会被拾取。

    3 回复  |  直到 7 年前
        1
  •  0
  •   Kind Stranger    7 年前

    os.listdir 列出目录中的所有内容。这包括文件和目录。我假设您第一次只需要目录(以生成 email_dirs )第二次只归档(生成 emails

    def make_Dictionary(root_dir):
        # # # Check for only directories # # #
        emails_dirs = [os.path.join(root_dir,f) for f in os.listdir(root_dir) if os.path.isdir(f)]    
        all_words = []       
        for emails_dir in emails_dirs:
            # # # Check for only files # # #
            emails = [os.path.join(emails_dir,f) for f in os.listdir(emails_dir) if os.path.isfile(f)]
            for mail in emails:
                with open(mail) as m:
                     for line in m:
                         words = line.split()
                         all_words += words
        dictionary = Counter(all_words)
        list_to_remove = dictionary.keys()
    
        for item in list_to_remove:
            if item.isalpha() == False: 
                del dictionary[item]
            elif len(item) == 1:
                 del dictionary[item]
        dictionary = dictionary.most_common(4000)
    
        np.save('dict_movie.npy',dictionary) 
    
        return dictionary
    
    root_dir = sys.path[0]
    dictionary = make_Dictionary(root_dir)
    
        2
  •  0
  •   Stephen Rauch ajay singh koranga    7 年前

    emails_dirs

    emails_dirs = [os.path.join(root_dir,f) for f in os.listdir(root_dir)
                   if os.path.isdir(os.path.join(root_dir,f))]  
    
        3
  •  0
  •   gansteed    7 年前

    您正在使用 os.path.join(rootdir,f)