Python file operations,
The file object is created using the open function. The operations on the file are divided into three steps:
1. open the file and obtain the file handle.
2. read/write file content through file handle
3. close the file.
Note:
Python file operation 1,File Operation Process 1. open the file, get the file handle and assign it to a variable 2. Operate the file through the handle 3. close the file to open the file
Open () returns a file object. The basic syntax format is as
One, python in the file, folder operations often used in the OS module and Shutil module common methods.1. Get the current working directory, that is, the directory path of the current Python script work: OS.GETCWD ()2. Return all files and
1[Email Protected]:/home/liujianzuo/python/test#ls2 passwd rc.local test13[Email Protected]:/home/liujianzuo/python/test#py test1-r EXIT exit/home/liujianzuo/python/test/rc.local4 a total of 0 lines have been modified. 5[Email
1. Open the Mode list#Open the Mode list:#W opens in write mode,#a opens in Append mode (starting with EOF and creating a new file if necessary)#r+ Open in read-write mode#w+ Open in read/write mode (see W)#A + opens in read/write mode (see a)#RB
File operations also read operations and write operations, we one by one.Read File operationIn fact, and in the daily use of the file is the same, but to read files or write files, the first thing is to open the file .FileRead = open ('/test.txt'R')
Two modules are involved: OS module and Shutil moduleOS.GETCWD (): Gets the current working directory, which is the directory path of the current Python script workOs.listdir (): Returns all files and directory names under the established
From the second version of Python's core programming, this essay is just for the sake of making a deeper impression.Environment OS X python version 2.7.101 #!/usr/bin/env python2 #--*--coding:utf-8--*--3 4 'maketextfile.py--Create text file'5 6
#coding: GBK#md5SHA1 file Check value calculation# How to use: python file . PY Validating FilesImport HashlibImport SysImport OSdefmd5_sum (filename): # Check Value Methodfd=open (filename, "RB") # Open FileFd.seek (0) # move the file hit tag to
You can't remember the API. I used this when I wrote it last night, but I didn't remember it, so I simply sorted it out to make it easy for friends who needed it.
Operations on files and folders (File operation functions) in python involve the OS
This article describes how to use python to delete nginx cache files (python file operations). For more information, see enter parameters for the next call, such as: www.bitscn.com/#delete the cache on the first page of www.bitscn.com.
The code
First: You need to determine the stored path of the compiled Python program (that is, in which disk, in which file)Second: Open the cmd command, enter the disk of the program, hit Enter, (for example, it is stored in the E-drive, enter "E:", hit the
File (' Test.txt ', ' r ') read-only mode, is also the default way. (If the Test.txt file does not exist, it will error.) ) file (' Test.txt ', ' W ') write a file. (If you do not have this file, a file will be created in the current
I. file systems and documentation1. File systems and filesA file system is the method and data structure used by the OS to clear files on a disk or partition-the method of organizing files on diskcomputer files (or files, computer archives, files)
Procedure for file operation
Open the file, get the file handle and assign a value to a variable
Manipulating a file with a handle
Close File
The existing files are as followsBeautiful is better than ugly. Beauty is better
Tag: Lock file read-only operation Python path exists open files lineI. Read and write files
1. Locate the file
File path:./test.py
2. Open File
Open (' File path ', ' mode ')
Mode:
R, open in read-only mode
W, open
in many cases, you will want your program to interact with the user (possibly yourself). You'll get input from the user and print some results. We can use Raw_input and print statements separately to accomplish these functions. For output, you can
File read-write open (Encoding=utf-8) #pyton2, Python3fie () #python2r read-only w write a append write, followed by + words, is read and write, read, append read and write u put all the files inside the newline character, are changed to \ NB Binary.
Procedure for file operation
Open the file, get the file handle and assign a value to a variable
Manipulating a file with a handle
Close File
Basic operations
12345678
f =open(‘lyrics‘) #打开文件first_line =
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.