简体   繁体   English

Python 3.5.2 FileNotFoundError: [Errno 2] 没有这样的文件或目录:'test.txt'

[英]Python 3.5.2 FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'

Here is a sample of the code that's causing my issue:这是导致我的问题的代码示例:

import string

letters = string.ascii_lowercase       

offset = ord('a')


def countFrequency(file_handle):
"""Traverse a file and compute the number of occurences of each letter
return results as a simple 26 element list of integers."""
results = [0] * len(letters)     
for line in file_handle:   #read line from file
    for char in line:      #read char by char from above line
        char = char.lower()    #cnverting char in lowercase
        if char in letters:    #checking if char exist i letters
            results[ord(char) - offset] += 1

return results


#sorting the both lists frequency and letter list    
def sort(ltr,frequency):
for x in xrange(len(frequency)):
    for y in xrange(len(frequency)-1):
        if frequency[y]<frequency[y+1]: # on true swap values
            t=frequency[y+1]
            frequency[y+1]=frequency[y]
            frequency[y]=t
            t=ltr[y+1]
            ltr[y+1]=ltr[y]
            ltr[y]=t

#print the sorted results
for i in xrange(len(ltr)):
    if(frequency[i]==0):
        break;
    print "%s=%d" % (ltr[i], frequency[i])



if __name__ == "__main__":
filename = str(input('Enter file name(e.g. test.txt):'))
#file path
sourcedata = open(filename)
#fucntion return frequency of letters
frequency = countFrequency(sourcedata)
#creating list of letters (a-z) 
ltrs= [0] * len(letters)
for i in xrange(len(frequency)):
   ltrs[i]=chr(i + ord('a'))
#sorting the result in descending order
sort(ltrs,frequency)

I am supposed to write a Python program that prompts for an input filename.我应该编写一个提示输入文件名的 Python 程序。 The program should open the named file and calculate the frequency of each letter in it, ignoring case.程序应该打开命名文件并计算其中每个字母的频率,忽略大小写。 The resulting table should be output to the screen in sorted order.结果表应按排序顺序输出到屏幕。 By “sorted order” I mean that the most frequent letter should be displayed first, followed by the second most frequent letter, and so on. “排序顺序”是指最频繁的字母应该首先显示,然后是第二个最常见的字母,依此类推。 This is what I got so far but when I try running it and inputting the filename I receive:这是我到目前为止所得到的,但是当我尝试运行它并输入我收到的文件名时:

sourcedata = open(filename) #file path FileNotFoundError: [Errno 2] No such file or directory: 'filename'

What could be casing this error?什么可能导致这个错误? How do I resolve this issue?我该如何解决这个问题?

Providing you're using the full, correct path, you can try:如果您使用的是完整、正确的路径,您可以尝试:

import os
filename = input('Enter file name(e.g. test.txt):')
filename = os.path.join(filename)
sourcedata = open(filename)

Additionally, if you're using Python 3+, print "%s=%d" % (ltr[i], frequency[i]) , will not work;此外,如果您使用的是 Python 3+,则print "%s=%d" % (ltr[i], frequency[i])将不起作用; it should be:应该是:

print('{}={}'.format(ltr[i], frequency[i]))

Use the absolute path (use /Path/to/your/file.txt instead of file.txt).使用绝对路径(使用 /Path/to/your/file.txt 而不是 file.txt)。 Using the absolute path is much better than using the relative path because you will not get the FileNotFoundError and it will work in Python 2 and in Python 3. As @Trenton_m has said,使用绝对路径比使用相对路径好得多,因为您不会得到 FileNotFoundError 并且它可以在 Python 2 和 Python 3 中工作。正如@Trenton_m 所说,

print "%s=%d" % (ltr[i], frequency[i])

will not work in python 3. You should use:在 python 3 中不起作用。你应该使用:

print('{}={}'.format(ltr[i], frequency[i]))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Errno 2没有这样的文件或目录:&#39;/storage/test.txt&#39; - Errno 2 No such file or directory: '/storage/test.txt' txt:FileNotFoundError:[Errno 2]没有这样的文件或目录 - txt: FileNotFoundError: [Errno 2] No such file or directory FileNotFoundError:[Errno 2] 没有这样的文件或目录:'test_text.txt' - FileNotFoundError: [Errno 2] No such file or directory: 'test_text.txt' Anaconda (Python) VSCode: FileNotFoundError: [Errno 2] 没有这样的文件或目录:&#39;file.txt&#39; - Anaconda (Python) VSCode: FileNotFoundError: [Errno 2] No such file or directory: 'file.txt' Python [FileNotFoundError: [Errno 2] No such file or directory: 'sample.txt'] 错误 - Python [FileNotFoundError: [Errno 2] No such file or directory: 'sample.txt'] error 文件 txt: FileNotFoundError: [Errno 2] 没有那个文件或目录 - File txt: FileNotFoundError: [Errno 2] No such file or directory FileNotFoundError:[Errno 2] 没有这样的文件或目录:'file1.txt' - FileNotFoundError: [Errno 2] No such file or directory: 'file1.txt' Python 3-FileNotFoundError:[Errno 2]没有这样的文件或目录 - Python 3 - FileNotFoundError: [Errno 2] No such file or directory python:FileNotFoundError:[Errno 2]没有这样的文件或目录 - python: FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError:[错误2]没有这样的文件或目录 - Python FileNotFoundError: [Errno 2] No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM