Ubuntu Python os.path.join错误读取文件

时间:2018-02-20 19:34:33

标签: python-3.x ubuntu-16.04

在Windows上,这很好用,但是,当我尝试在Ubuntu中读取文件时,我收到错误,"没有这样的文件或目录。"一切都保持不变。

import os
def load_ablist(shopID):
    cwd = os.getcwd()
    path = os.path.join(cwd, "ABlists/%i.txt" % shopID)
    f = open(path, mode='r')
    ablist = (f.read()).split(sep="\n")
    f.close()
    return ablist

1 个答案:

答案 0 :(得分:2)

当我尝试运行您的代码时,它会告诉我这个错误。

path = os.path.join(cwd, "ABlists/%i.txt" % (shopID))
TypeError: %i format: a number is required, not str



import os

def load_ablist(shopID)
    cwd = os.getcwd()
    path = os.path.join(cwd, "ABlists/%s.txt" % str(shopID))
    f = open(path, mode='r')
    ablist = (f.read()).split(sep="\n")
    f.close()
    return ablist

load_ablist(123)
这对我有用。

path = os.path.join(cwd, "ABlists/%s.txt" % str(shopID))

你能尝试一下吗?