我正在嘗試編寫一個程序來幫助我理解如何讀取和寫入文本文件。我理解它的基本思想,但是當我嘗試將它們放入一個模塊中時,我無法讓它工作並獲得TypeError:期望的str,字節或os.PathLike對象,而不是第63行中列出的printData中的open (a)作爲f:有人能解釋我做錯了什麼嗎?無法顯示在Python列表中加載的文本文件
### Subprogram getData(fn)
# This will load the string into a list
def getData(fn):
# Opens the file and enables read
with open(fn, "r") as ins:
# Creates the list to load the string
list = []
for line in ins:
# Appends the string into the list
list.append(line)
return list
# ==============================================================================
### Subprogram printData(a)
# Displays the string
def printData(a):
# Opens the file
with open(a) as f:
for line in f:
# Displays the content of the string
print(line, end = "")
# ==============================================================================
def main():
# Declare text[] and array
text = []
# Assigns the fileName to data.txt
fileName = "data.txt"
# Calls saveDate() and assigns the string
saveData(fileName, "This is a test")
# Calls getData()
textIn = getData(fileName)
# Calls printData()
printData(textIn)
main()
saveData丟失 – aless80