Python file operation

Open\create file

In python, using the open function, you can open an existing file or create a new file. By default, the new file is created in the folder where the current program is located.

Format: open (file name, access mode), for example

f =open('test.txt','w') #Open the file in write mode, create a new file if it does not exist

If you do not specify the mode when creating a new file, the default is to read mode, but if the new file does not exist, an error will be reported.

# Write
'''
f =open('test.txt','w') #Open the file in write mode, create a new file if it does not exist

f.write("hello python") #Write string to file

f.close() #Close the file operation every time
'''

'''
# Read, read method, read the specified character, locate at the head of the file at the beginning, move backward the specified number of characters each time it is executed
f =open("test.txt","r")

str = f.read(5) #Specify five characters to read
print(str)

str = f.read(5) #Read five more characters, use the file pointer, read backwards in turn, not every time from the beginning
print(str)

f.close()'''

# Read, readline()、readlines()method
f =open("test.txt","r")

# str = f.readline() #Starting from the first line, read only one line at a time
# print(str)
#
# str = f.readline() #Read one more line down
# print(str)

strs = f.readlines() #Read all rows. Output as a list, with each row as an element in the list
# Use enumeration to output the line number of each line
for num,str inenumerate(strs):print("First%d line:%s"%(num+1,str))

f.close()

File Rename##

The rename() in the os library can complete the renaming of files.
Format: rename (the file name to be modified, the new file name)

import os
os.rename("Graduation thesis.txt","Graduation thesis-final version.txt")|

Delete Files##

The remove() in the os module can complete the file deletion operation
Format: remove (the file name to be deleted)

import os
os.remove("Graduation thesis.txt")

Create\Delete Folder##

import os
os.mkdir("Directory name")
os.rmdir("Directory name")

Get the current directory##

import os 
os.getcwd()

Recommended Posts

Python file operation
Quick start Python file operation
Python file operation basic process analysis
python_ file processing
python operation kafka
Python file and directory operation code summary
Python operation SQLite database
Python operation yaml instructions
Python automated operation and maintenance 2
Python operation Excel merge cells
Python negative modulus operation example
Python implements TCP file transfer
Python tornado upload file function
Python automated operation and maintenance 1
Python file read and write operations
Python preliminary implementation of word2vec operation
How to write python configuration file
Python list comprehension operation example summary
Implementation of python selenium operation cookie
Python implements ftp file transfer function
Some examples of python operation redis
Example operation of python access Alipay
How Python operates on file directories
The operation of python access hdfs
Python handles operation code to execl
Python multithreading
A practical guide to Python file handling
Python CookBook
Python FAQ
Python3 dictionary
Python3 module
python (you-get)
Python string
Python descriptor
Python basics 2
Python exec
Python realizes batch modification of file names
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python multitasking-coroutine
Python overview
python introduction
Python analytic
Python basics
07. Python3 functions
Python basics 3
Python multitasking-threads
"Python hands-on learning" Python processing .mat file
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string