def creation():
#create file and ask name of file
#1st input - file name is loki.txt
#2nd input what do we want to write inside txt file
f = open(input("Mink?niminen tiedosto luodaan?: "), "w")
f.write(input('Mit? kirjoitetaan tiedostoon?:'))
f.close()
creation()
def main():
#file read and printing
tiedosto = open("loki.txt","r")
text = tiedosto.read()
print(text)
f.close()
return
#last print the name of file and txt inside of txt
print("Luotiin tiedosto", f.name, 'ja siihen tallennettiin teksti:', text)
我的問題是如何將該檔案名列印到最后一行。錯誤描述是:NameError name 'f' is not defined
uj5u.com熱心網友回復:
def creation():
fileName = input("Mink?niminen tiedosto luodaan?: ")
if fileName:
with open(fileName, "w") as f:
txt = input('Mit? kirjoitetaan tiedostoon?:')
f.write(txt)
return fname
createdFile = creation()
print(f"Luotiin tiedosto {createdFile`enter code here`} ja siihen tallennettiin teksti: {txt}")
uj5u.com熱心網友回復:
一種選擇是將名稱保存在變數中并將其回傳creation:
def creation():
fname = input("Mink?niminen tiedosto luodaan?: ")
f = open(fname, "w")
f.write(input('Mit? kirjoitetaan tiedostoon?:'))
f.close()
return fname
fname = creation()
print("Luotiin tiedosto", fname, 'ja siihen tallennettiin teksti:', text)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/466465.html
上一篇:如何從檔案中獲取數字
