Python-based File Operation: Difference between revisions
(Redirected page to Python File Operation) |
No edit summary |
||
Line 1: | Line 1: | ||
A [[Python-based File Operation]] is a [[file operation]] that is a [[Python operation]]. | |||
* <B>Context:</B> | |||
** It can range from being a [[Python File Open Operation]] to being a [[Python File Close Operation]] (on [[Python file descriptor]]s). | |||
** 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]]. | |||
* <B>Example(s):</B> | |||
** <code>with open('testit.txt') as f: <BR> inputFileText = f.read() <BR> print(inputFileText)</code> | |||
** <code>infile = open('file.csv')<BR>data_list = [line.rstrip().split(',') for line in infile.readlines()]</code> | |||
** <code>pandas.read_csv("file.csv.zip", compression='zip', header=0, sep=',', quotechar='"', skiprows=0) ;</code> | |||
** <code>pandas.to_csv("out.tsv", sep='\t', index=True, cols=["col3","col1"], encoding='utf-8') ;</code> | |||
df.to_csv(file_name, sep='\t', encoding='utf-8') | |||
** <code>allFiles = glob.glob("./*.csv.zip")</code> | |||
* <B>Counter-Example(s):</B> | |||
** [[Scala File Operation]], [[R File Operation]], [[Perl File Operation]], [[Hive File Operation]], ... | |||
** a [[Python Data Management Operation]] (e.g. [[Python random sampling|sampling]]), ... | |||
** a [[Python Data Conversion Operation]]. | |||
* <B>See:</B> [[Python Coding Example]]. | |||
---- | |||
---- | |||
==References == | |||
* https://docs.python.org/2/tutorial/inputoutput.html | |||
===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 <code>with</code> statement; see [[Python Programming/Context Managers|context managers]] section. <P> 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 <code>File.close()</code>, but this is error-prone and requires great care to be exception safe. In interactive use using explicit <code>open()</code> and <code>File.close()</code> results in immediate evaluation, instead of the delayed evaluation of using a <code>with</code> statement. | |||
---- | |||
== Examples == | |||
; Reading & Writing Files | |||
Just reading from a file | |||
<pre> | |||
open my $io, "<-" or die "NO STDIN: $!" ; | |||
</pre> | |||
Mixed Read & Write | |||
<pre> | |||
... | |||
</pre> | |||
; Subroutine-based | |||
Subroutine to open a file for writing and write into [[it]]. | |||
<pre> | |||
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: | |||
... | |||
</pre> | |||
Subroutine to open a file for writing and write into [[it]]. | |||
<pre> | |||
... | |||
</pre> | |||
; File Open Codes | |||
<pre> | |||
... | |||
</pre> | |||
; File Management | |||
Rename a File | |||
<pre> | |||
... | |||
</pre> | |||
Delete a file | |||
<pre> | |||
... | |||
</pre> | |||
Choose between STDIN or file | |||
<pre> | |||
... | |||
</pre> | |||
---- | |||
; Directory-based operations | |||
Read a directory's files (shortcut with pattern matching). | |||
<pre> | |||
... | |||
</pre> | |||
Load all files in a directory into the @files array | |||
<pre> | |||
... | |||
</pre> | |||
a subroutine approach | |||
<pre> | |||
... | |||
</pre> | |||
---- | |||
; JSON-based operations | |||
<pre> | |||
... | |||
</pre> | |||
---- | |||
__NOTOC__ | |||
[[Category:Concept]] |
Revision as of 17:26, 24 August 2016
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') as f:
inputFileText = f.read()
print(inputFileText)infile = open('file.csv')
data_list = [line.rstrip().split(',') for line in infile.readlines()]pandas.read_csv("file.csv.zip", compression='zip', header=0, sep=',', quotechar='"', skiprows=0) ;
pandas.to_csv("out.tsv", sep='\t', index=True, cols=["col3","col1"], encoding='utf-8') ;
df.to_csv(file_name, sep='\t', encoding='utf-8')
allFiles = glob.glob("./*.csv.zip")
- 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
with
statement; 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 awith
statement.
- 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
...