Skip to main content

Some very basic routines for file IO in Python

Project description

PyFileIO

Some very basic routines for file IO in Python

Installation

Install via pip3:

pip3 install PyFileIO --user

or from this repo:

git clone https://github.com/mattkjames7/PyFileIO
cd PyFileIO 

#either this
python3 setup.py install --user

#or this
python3 setup.py bdist_wheel
pip3 install dist/PyFileIO-X.X.X-py3-none-any.whl

where X.X.X is the version created.

Usage

This module contains a few different methods of loading/saving data.

Loading/Saving Objects

This effectively uses pickle to load and save physical objects, e.g.:

import PyFileIO as pf

#save an object
pf.SaveObject(obj,'/path/to/some/file.bin')

#load an object
obj = pf.LoadObject('/path/to/some/file.bin')

Loading/Saving ASCII Data

Text files may be created and read directly:

#saving text
text = 'some text, can be an array\n or just a single string'
pf.WriteASCIIFile('filename.txt',text)

#reading text
text = pf.ReadASCIIFile('filename.txt')

We can also use ASCII files to load csv files and save data stored in a simple numpy.recarray:

#read a csv file, which contains a header - dtype will be worked out automatically
data = pf.ReadASCIIData('somedata.csv')

#we can also save data
pf.WriteASCIIData('newfile.dat',data)

NOTE: this will only work with simple dtypes

Loading/Saving Binary Data

Pure binary data may be written to files using the following functions:

#open a file
f = open('filename.bin','wb')

#save some stuff
ScalarToFile(x,'int64',f)		#save a single scalar integer
ArrayToFile(y,'float32',f) 		#save a floating point array
ListArrayToFile(z,'int32',f)	#save a list of integer arrays
StringToFile(s,f)				#save a string to file

#close the file
f.close()

We can also read the data back (remembering to use the correct dtypes!):

#open a file
f = open('filename.bin','rb')

#read the stored data
x = ScalarFromFile('int64',f)		#read a single scalar integer
y = ArrayFromFile('float32',f) 		#read a floating point array
z = ListArrayFromFile('int32',f)	#read a list of integer arrays
s = StringFromFile(f)				#read a string from file

#close the file
f.close()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

PyFileIO-0.0.5-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file PyFileIO-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: PyFileIO-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.0 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.4

File hashes

Hashes for PyFileIO-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9924df97bf5a2af6d469d57a053827c758fe64c2a2ff2647ba541705156288ec
MD5 0572c56e40c0915bedb388d06f5acba9
BLAKE2b-256 c476c6756c0c71998338b02545579297559d9e1ce0eded0f0121814c42b80b66

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page