Python-based File Operation: Difference between revisions
m (Text replacement - "** ..." to "** …")  | 
				m (Text replacement - " "<" to " “<")  | 
				||
| Line 22: | Line 22: | ||
== References ==  | == References ==  | ||
* https://docs.python.org/2/tutorial/inputoutput.html  | * https://docs.python.org/2/tutorial/inputoutput.html  | ||
| Line 38: | Line 37: | ||
<pre>  | <pre>  | ||
  open my $io,   |   open my $io, “<-" or die "NO STDIN: $!" ;  | ||
</pre>  | </pre>  | ||
Revision as of 04:27, 8 May 2024
A Python-based File Operation is a file operation that is a Python operation.
- Context:
- It can range from being a Python File Open Operation to being a Python File Close Operation (on Python file descriptors).
 - It can range from being a Python File Read Operation to being a Python File Write Operation (such as a Python file append operation).
 - It can range from being a Python File Metadata Operation.
 
 - Example(s):
with open('testit.txt', encoding="utf8") as f:
inputFileText = f.read()
mylist = f.read().splitlines()
print(inputFileText)
mylist[0:10]infile = open('file.csv', encoding="utf8")
data_list = [line.rstrip().split(',') for line in infile.readlines()]allFiles = glob.glob("./*.csv.zip")- a Pandas-based File Operation, such as:
df = pandas.read_csv("./file.csv.zip", compression='zip', header=0, sep=',', quotechar='"', skiprows=0) ;pandas_df.to_csv("out.tsv", sep='\t', index=True, cols=["col3","col1"], encoding='utf-8') ;
 - …
 
 - Counter-Example(s):
 - See: Python Coding Example.
 
References
2014
- http://en.wikibooks.org/wiki/Python_Programming/Files
- Files, specifically file handles, are an important example of resources, and thus should generally be managed using the 
withstatement; see context managers section.In rare cases – namely when a file is not only used within a single block of code – it is necessary to do manual resource management using
File.close(), but this is error-prone and requires great care to be exception safe. In interactive use using explicitopen()andFile.close()results in immediate evaluation, instead of the delayed evaluation of using awithstatement. 
 - Files, specifically file handles, are an important example of resources, and thus should generally be managed using the 
 
Examples
- Reading & Writing Files
 
Just reading from a file
open my $io, “<-" or die "NO STDIN: $!" ;
Mixed Read & Write
...
- Subroutine-based
 
Subroutine to open a file for writing and write into it.
import csv
filename = sys.argv[1]
with open(filename, 'rb') if sys.argv[1] is not "-" else sys.stdin as f:
   reader = csv.reader(f,dialect="excel-tab")  # creates the reader object
   for row in reader:
      ...
Subroutine to open a file for writing and write into it.
...
- File Open Codes
 
...
- File Management
 
Rename a File
...
Delete a file
...
Choose between STDIN or file
...
- Directory-based operations
 
Read a directory's files (shortcut with pattern matching).
...
Load all files in a directory into the @files array
...
a subroutine approach
...
- JSON-based operations
 
import json
with open("data_file.json", "w") as write_file:
    json.dump(data, write_file)
print(json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4))
 {
    "4": 5,
    "6": 7
 }
...