程序:
from 方法文件 import 方法:
filename = 'xiaoshuo.txt'
方法(filename)
找不到xiaoshuo.txt文件
在xiaoshou.txt文件前加一个相对路径有就可以运行
但是,着三个文件本来就在同一目录下,怎么会还需要加相对路径
不需要加相对路径,该如何操作?
txt
test,
linux,
linux_arm,
linux_arm64
cx.py
# 判断txt文件中有多少个单词,单词以逗号分隔
def count_words(filename):
with open(filename) as f:
words = f.read().split(',')
return len(words)
t.py
import cx
if __name__ == '__main__':
print(cx.count_words('xiaoshuo.txt'))
切换到3个文件所在的路径,让这个路径作为当前工作路径,再试下运行py文件
把文件放到python路径中试试