Python-based File Operation: Difference between revisions

From GM-RKB
Jump to navigation Jump to search
m (Text replacement - ". ----" to ". ----")
No edit summary
Line 7: Line 7:
** <code>with open('testit.txt', encoding="utf8") as f: <BR> &nbsp; &nbsp; &nbsp; inputFileText = f.read() <BR> &nbsp; &nbsp; &nbsp; mylist = f.read().splitlines() <BR> print(inputFileText)<BR> mylist[0:10]</code>
** <code>with open('testit.txt', encoding="utf8") as f: <BR> &nbsp; &nbsp; &nbsp; inputFileText = f.read() <BR> &nbsp; &nbsp; &nbsp; mylist = f.read().splitlines() <BR> print(inputFileText)<BR> mylist[0:10]</code>
** <code>infile = open('file.csv', encoding="utf8")<BR>data_list = [line.rstrip().split(',') for line in infile.readlines()]</code>
** <code>infile = open('file.csv', encoding="utf8")<BR>data_list = [line.rstrip().split(',') for line in infile.readlines()]</code>
** <code>df = pandas.read_csv("file.csv.zip", compression='zip', header=0, sep=',', quotechar='"', skiprows=0) ;</code>
** <code>pandas_df.to_csv("out.tsv", sep='\t', index=True, cols=["col3","col1"], encoding='utf-8') ;</code>
** <code>allFiles = glob.glob("./*.csv.zip")</code>
** <code>allFiles = glob.glob("./*.csv.zip")</code>
** a [[Pandas-based File Operation]], such as:
*** <code>df = pandas.read_csv("./file.csv.zip", compression='zip', header=0, sep=',', quotechar='"', skiprows=0) ;</code>
*** <code>pandas_df.to_csv("out.tsv", sep='\t', index=True, cols=["col3","col1"], encoding='utf-8') ;</code>
** ...
* <B>Counter-Example(s):</B>
* <B>Counter-Example(s):</B>
** [[Scala File Operation]], [[R File Operation]], [[Perl File Operation]], [[Hive File Operation]], ...
** [[Scala File Operation]], [[R File Operation]], [[Perl File Operation]], [[Hive File Operation]], ...

Revision as of 23:44, 12 February 2022

A Python-based File Operation is a file operation that is a Python operation.



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 explicit open() and File.close() results in immediate evaluation, instead of the delayed evaluation of using a with statement.


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
 }

...