Python-based files read and write this section
Some methods of files and directories in the OS module
Operation of the file
Operation of the Directory
Some methods of files and directories in the 1.os moduleSome of the ways in which Python operations files and directories can use OS modules are as follows:
Get the current working directo
Python read/write configuration files have very powerful functions in practical applications, and there are also quite simple operation solutions in actual operations, the following is an introduction to the specific scheme of reading and writing configuration files in python. I hope you will gain some benefits after reading the following articles.
The
Data type: points to a specific file. Whether it's text or documents. or other media files.How to declare: Open (' path ', ' mode ', encoding = ' encode ');(1) Path example: ' C:\\data\\temp.txt ' slash needs to be escaped, so write 2 slashes. Add R ignore escape R ' C:\Data\temp.txt '(2) modeThere are 2 types of files under the distinction"1" textMode hasR Read Read reading textW Write write writes the tex
Python provides the necessary functions and methods for basic file operation by defaultHow to open the file:Open (Name[,mode[buf]])Name: File pathMode: Open modeBUF: Buffer buffering sizeFile read mode:Read ([size]): Read file (read size byte, read all by default)ReadLine ([
Python uses the list or dict field mode to read files,
Preface
Python is a powerful tool for processing text data. It is extremely simple to read, split, filter, and convert, so developers do not need to consider complicated stream file processing processes (compared with JAVA, hey ). Some complex text data processing
How to read large files using Python
Background
When I recently processed a text File (the File size is about 2 GB), there was a memoryError error and the File Reading was too slow. Later I found two methods for fast Large File Reading, this article describes the two read methods.
Preparations
When talking about "text processing", we usually refer to the content
Example of how to read and write csv files using Python,
This example describes how to read and write csv files in Python. We will share this with you for your reference. The details are as follows:
Create a csvdata.csv file with the following data:
The Code is as follows:
# Coding: utf-8import csv #
1, Function Introduction1.1. Read the configuration file-read (filename) reads INI file contents directly-sections () gets all the sections and returns as a list-options (section) gets all the option of the section-items (section) gets all the key-value pairs of the section-get (section,option) Gets the value of option in section, returned as a string type-getint (section,option) Gets the value of option in
Read and Write XML in Python
(Original converted from http://zc0604.iteye.com/blog/1520703)
When using python for development, because Python's open-source ecosystem is very powerful, many class libraries are often used to implement the same function, developers are also faced with how to choose the best class library as a tool to assist development. Thi
1. Open function Syntax:Python opens the file through the open function, creating a connection between the program and the file. Open function Syntax: Open (filename[,mode[,buffering]])where filename refers to the name of the file to be manipulated, consisting of the file name and path, which need to be quoted. mode is a parameter for operating modes, including read, write, append and so on. The buffering is a buffer parameter that controls the buffer
# This is a learning note for the Liaoche teacher Python tutorial1. OverviewRead-write files are the most common IO operations. Python has built-in functions for reading and writing files, and the usage is compatible with C.The ability to read and write files on disk is provided by the operating system, and the modern operating system does not allow the normal pr
True, otherwise false is returned.If the path parameter exists and is a folder, calling Os.path.isdir (path) returns True, otherwise false is returned.Using the os.path.exists () function, you can determine whether a DVD or flash drive is currently connected to your computer.>>> os.path.exists ('/tmp/1.txt ') true>>> os.path.isfile ('/tmp/1.txt ') true>>> Os.path.isdir ('/tmp/1.txt ') FalseFile read and write processPlain text files contain only basi
File structure:Db.ini configuration file for placing DB informationfile [Gloryroad] is section informationThe dbname below is the option informationUiobjectmap.ini placing configuration information for accessing the WebConfigure the XPath element information used-to achieve separation of data and programsThe first step reads the configuration filePlace the configuration file in the directory where the current script is located#encoding =utf-8Import ConfigparserImport OSImport PlatformIf Platform
command: OS. system () reads and sets the environment variable: OS. getenv () and OS. putenv () indicates the row Terminator used by the current platform: OS. linesep Windows uses '\ r \ n', Linux uses' \ n', and Mac uses '\ R' to indicate the platform you are using: OS. name is 'nt 'for Windows, while for Linux/Unix users, it is 'posix' renamed: OS. rename (old, new) creates a multilevel Directory: OS. makedirs (r "c: \ python \ test") to create a s
Copy CodeThe code is as follows:
Import OS
Import Codecs
Filenames=os.listdir (OS.GETCWD ())
Out=file ("Name.txt", "W")For filename in filenames:Out.write (Filename.decode ("gb2312"). Encode ("Utf-8"))Out.close ()
Writes the current directory and file name of the executing file to the Name.txt file and saves it in the UTF-8 formatIf you save with ANSI encoding, write with the following code:Copy the Code code as follows:
Out.write (filename)
Open the file and write
Refer to the Codecs modu
point the bytes is the binary form of the data, you can write directly to the file such as Binfile.write (bytes)
Then, when we need to, we can read it again, Bytes=binfile.read ()
Then decode the python variable by struct.unpack ()
A,b,c,d=struct.unpack (' 5s6sif ', bytes)
' 5s6sif ' is called FMT, which is a formatted string, consisting of numbers plus characters, 5s representing a 5-character string, 2i,
A, 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 current Python script work directory path: OS.GETCWD ()2. Returns all files and directory names under the specified directory: Os.listdir ()3. function to delete a file: Os.remove ()4. Delete multiple directories: Os.removedirs (r "C:\
the size are deleted after truncation. If the size of the file is larger, depending on the system may not change the file, it may be 0 files to the corresponding size, it may be some random content to add. fp= ' E:\\tmp\\03.txt ' f1=open (FP, ' r+ ') line=f1.readline () print linef1.truncate () print F1.tell () f1.close () Second, read and write to the file 1, normal read and write operation (1)
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.