File object methods commonly used in document input and output:
Open : Returns a new file object, calling the method on the object to do anything to the file
ReadLine : read a line you give oh if you include the end of the line break
Write : writing data to a file
Close : close File object;
Example: Read the Work.txt file in E-drive
f = open (' work.txt ') line =f.readline () print (line) f.close ()
If you set a parameter to ReadLine n then read n characters not set then read only one line including the end of the line break
Output Result:
What is Words (A tribute to Chris Medina)-Ameritz-tribute
We read all the data in the file through a while loop:
f = open (' e:/work.txt '), line = F.readline (), while Line:print (line); line = F.readline (); F.close ();
The program loops through all the rows in the file work.txt if the program is on a dataset like this, such as an input line, number, string, loop We are often called iterations ; Python provides a dedicated statement for iterating over all the content items of the output file:
For on open (' E:/work.txt '): print (line);
To perform a write operation on a file:
f = open (' E:/work.txt ', ' W '); #w: The file open Mode W indicates an executable write operation if F:f.write (' What is Words '); f.close;
Output Result:
What is Words
Add:
Methods in file operation:
read: Reads n characters from a file acts all characters when given a parameter
seek : move pointer first parameter offset second parameter: from where to start 0 1 current position 2 end
writelines : writing a sequence of strings
Mode of opening file in file operation:
r: Read mode open
W: Write to overwrite content in original file
A: Write to open in the original content to perform an append write operation
In PHP , file manipulation is similar to Python:
File manipulation functions:
fopen: Open File Common open way:
R: Read-only mode open
r+: Read and write mode open
W: pointer start position, write open file does not exist then create
w+: Start position of pointer, read and write open file does not exist then create
A: Pointer trailing position write open file does not exist then create
A +: pointer trailing position read-write mode open file does not exist then create
fread: Reading content
Fwrite: Write Content
Fgets: Get a row or n characters
Fgetc Get a Byte
Fseek: Move pointer 0 to initial position
ReadFile: Output a file
When you use Fread ($file, filesize) fgets fgetc and other methods to output content, use the Fseek method to move the pointer to the initial position if it is empty
For example:
$filename = ' E:/work.txt '; $file = fopen (filename, ' w+ '); $content = ' This was my house '; Fwrite ($file, $content); Echo ReadFile (filename); Output file Contents fseek ($file, 0); $str = Fread ($file, FileSize ($filename)); Only Fread example Var_dump ($STR); fclose ($file);
Summarize:
1,ptyhon file operation method;
2, the method for iterating the output in Python:
For line in Filedir:
3, PHP file output operation comparison;
This article is from the "Hong Dachun Technical column" blog, please be sure to keep this source http://hongdachun.blog.51cto.com/9586598/1759601
Python Basic file operations (file input and output)