Skip to main content
 首页 » 编程设计

python之IO错误 : [Errno 2] No such file or directory: '0.txt'

2024年10月17日13myhome

<分区>

我正在编写一个程序来从文件夹“set”中读取一组文本文件到列表中。我的代码是

def get_documents(): 
    path1 = "D:/set/" 
    texts = [] 
    listing1 = os.listdir(path1) 
    for file in listing1: 
        with open(file,'r') as f: 
            lines = f.read().splitlines() 
            texts.append(lines) 

但这给了我错误作为

with open(file,'r') as f: 
IOError: [Errno 2] No such file or directory: '0.txt' 

D:/set/ 中有文本文件,即 0.txt,1.txt,2.txt... 这个错误的原因是什么?