# # Read and write files using the built-in Python file methods # (For Python 3.x, be sure to use the ExampleSnippets3.txt file) def main(): # Open a file for writing and create it if it doesn't exist # f = open("textfile.txt","w+") #creating a variable "f", and calling the function open to open a file named textfile.txt # Open the file for appending text to the end # f = open("textfile.txt","a+") # write some lines of data to the file # for i in range(10): # f.write("This is line %d\r\n" % (i+1)) # close the file when done # f.close() # Open the file back up and read the contents f = open("textfile.txt","r") if f.mode == 'r': # check to make sure that the file was opened # use the read() function to read the entire file # contents = f.read() if __name__ == "__main__": main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question