Two ways to open a file

f = open("data.txt","r")   #Setting the file object

f.close() #Close file

#For convenience, and to avoid forgetting to close the file object, you can use the following instead

with open('data.txt',"r") as f:    #Setting the file object

    str =

Some simple operations on documents

1.Read files

f = open("data.txt","r")   #Setting the file object

str =     #Read all the contents of the txt file into the string str

f.close()   #Close the file

2.Read the entire file by line

#The first method

f = open("data.txt","r")   #Setting the file object

line = f.readline()

line = line[:-1]

while line:             #Until the file is read

    line = f.readline()  #Read a line of file, including line breaks

    line = line[:-1]     #Remove line breaks, or not

f.close() #Close file



#The second method

data = []

for line in open("data.txt","r"): #Set the file object and read each line of the file

    data.append(line)               #Add each line of the file to the list



#The third method

f = open("data.txt","r")   #Setting the file object

data = f.readlines()  #Read the file directly into the list by line, with the same effect as method 2

f.close()             #Close file

3.Write file

Simply write a string to txt

with open('data.txt','w') as f:    #Setting the file object

    f.write(str)                 #Writing a string to a file

4.List write file

data = ['a','b','c']

#Single-level list write file

with open("data.txt","w") as f:


Related articles

python how to add new content to the dictionary

Adding new content to the dictionary is done by adding new key/value pairs, modifying or deleting existing key/value pairs as shown in the following examples: #!/usr/bin/python dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

python how to get the current file path

Python method for getting the current path. import os,sys Use sys.path[0], sys.argv[0], os.getcwd(), os.path.abspath(__file__), os.path.realpath(__file__)

How to read csv files in python

Comma-Separated Values (CSV, sometimes called character-separated values because the separating characters can also be other than commas), whose files store tabular data (numbers and text) in plain text

How to output vertically in python

Example: The output is the following case H e l l o W o r l d This can be done using a for loop. for name in "Hello World": print(name) This can also be done using the join method print("\n".join("Hello World"))

python how to input a list with input function

In Python 3.0 onwards, keyboard input uses the input function >>> x=input >>> 123 123 There is no display on the command line, and the input 123 is directly assigned to x and printed. Using input alone can't solve most of the data processing, usually

How python dict is implemented

The dict object in Python is a primitive Python data type, stored as a key-value pair, whose Chinese name translates to dictionary, and as the name implies, it is highly efficient at finding the corresponding value by key name

Can python replace JavaScript?

python can replace JavaScript; Pyjamas can be used to achieve Python instead of JavaScript, Pyjamas is a Python ajax development framework that can be used instead of HTML and JavaScript to write web applications , you can reuse and import classes and mod

Python+AI to colorize old photos

Colorize old photos with NoGAN's image enhancement technique.NoGAN is a new type of GAN that takes the least amount of time for GAN training.

python reverse Dict

A very common dictionary task is if we have a dictionary and want to flip its keys and values, the keys will become the values and the values will become the keys