学习python并坚持基本问题。我有几个函数,第一个从用户输入中获取文件的名称。第二个函数应该采用openFile中给出的文件并计算字符数,但是我不得不在charcount函数中再次重新输入文件名。由于我还是新手,只是寻找最基本的解决方案,不在第二个功能中重新输入文件名。感谢
def openFile():
user_inp = raw_input(str(" "))
myfile = user_inp
fhandle = open(myfile,'r')
return fhandle
def charcount():
file1 = openFile()
charcount = 0
line = file1.readline()
while line != "":
charcount = charcount + len(line)
line = file1.readline()
file1.close()
return(charcount)
答案 0 :(得分:0)
在这两个函数之外获取用户输入,然后在需要时将其作为参数传递给每个函数。