2016-11-07 29 views
2

我想寫一些文字到文件中,並在這裏提交線是我的嘗試:的Python:寫文一行

text ="Lorem Ipsum is simply dummy text of the printing and typesetting " \ 
        "industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s," \ 
        " when an unknown printer took a galley of type and scrambled it to make a type specimen book." 
target = open("file", 'wb') 
target.writelines(text) 

,我得到一個空文件。 我該怎麼做?

+0

你也許忘了'.close()'後來的文件嗎? –

+0

也,你的文字只有一行。 –

+0

'target.close()'會保存它。 –

回答

2

這是如何打印到一個txt文件:

file = open("Exported.txt", "w") 
file.write("Text to write to file") 
file.close() #This close() is important 

另一種方式來做到這一點會是:

with open('Exported.txt', 'w') as file: 
    file.write("Text to write to file") 

這是我做了寫一個txt文件的程序:

import os.path 

def start(): 

    print("What do you want to do?") 
    print(" Type a to write a file") 
    print(" Type b to read a file") 
    choice = input("   -") 
    if choice == "a": 
     create() 
    elif choice == "b": 
     read() 
    else: 
     print("Incorrect spelling of a or b\n\n") 
     start() 


def create(): 

    print() 
    filename = input("What do you want the file to be called?\n") 
    if os.path.isfile(filename): 
     print("This file already exists") 
     print("Are you sure you would like to overwrite?") 
     overwrite = input("y or n") 
     if overwrite == "y": 
      print("File has been overwritten") 
      write(filename) 
     else: 
      print("I will restart the program for you") 
    elif not os.path.isfile(filename): 
     print("The file has not yet been created") 
     write(filename) 
    else: 
     print("Error") 





def write(filename): 
    print() 
    print("What would you like the word to end writing to be?") 
    keyword = input() 
    print("What would you like in your file?") 
    text = "" 
    filename = open(filename, 'w') 
    while text != keyword: 
     filename.write(text) 
     filename.write("\n") 
     text = input() 


def read(): 
    print() 
    print("You are now in the reading area") 
    filename = input("Please enter your file name:  -") 
    if os.path.isfile(filename): 
     filename = open(filename, 'r') 
     print(filename.read()) 
    elif not os.path.isfile(filename): 
     print("The file does not exist\n\n") 
     start() 
    else: 
     print("Error") 


start() 
+0

@Ashref如果我的代碼已經幫助,請注意,如果它已回答您的問題,請按下綠色勾號將其標記爲答案。然後其他有同樣問題的人也可以找到這個答案來幫助他們解決問題。謝謝! PS。我也確保upvote你的問題:) –

1

writelines需要一個可迭代(例如列表)的行,所以不要使用它。而你需要關閉該文件保存更改,這是最好用with語句來完成:

with open("file", 'wb') as target: 
    target.write(text) 
+0

是的,問題是關閉,我也改變了寫入和寫作,它工作正常。 – Ashref

1

您提供的代碼使用所需的行生成名爲file的文件。也許你打算把它保存爲「file.txt」。另外,'wb'標誌中的'b'通知代碼以二進制模式寫入文件(更多信息here)。如果您希望文件可讀,請嘗試使用'w'

最後是最好的做法,以訪問文件

text ="Lorem Ipsum is simply dummy text of the printing and typesetting " \ 
       "industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s," \ 
       " when an unknown printer took a galley of type and scrambled it to make a type specimen book." 
with open("file.txt", 'w') as f: 
    f.write(text) 
0

時,通過這種方式使用with語句,你應該直接關閉文件:

target = open("filename.txt", 'w') 
target.writelines(text) 
target.close() 

通過這種方式縮進後的文件關閉with執行完成後的塊:

with open("filename.txt", "w") as fh: 
    fh.write(text) 

更多信息: