parmed.utils.io module¶
Tools to aid in input/output within the parmed package
-
parmed.utils.io.
genopen
(name, mode='r')[source]¶ Opens a file, automatically detecting compression schemes by filename extension. Note, these files are opened in a way that always returns a string. This is an important distinction in Python 3 where many file-like objects return bytes instead of strings. This is detected and handled properly so that the object returned from this function is always string-based (so you cannot write or read bytes directly from a file opened via
genopen
).This routine also recognizes URLs and will read remote files when given a URL starting with either http:// or https://. Like with standard local file names, compression is automatically detected by filename extension, and both gzip and bzip2 files are supported.
- Parameters
- namestr
Name of the file to open or URL to a remote file to access
- modestr, optional
Whether to open the file to ‘r’ead, ‘w’rite, or ‘a’ppend. Default is ‘r’
- Returns
- filefile-like
A file-like object in the requested mode
Notes
Python’s BZ2File does not support writing to
append
mode (mode=’a’), so it is faked here. The entire file contents are read into memory and then written into a ‘new’ file with the same name as the original. As such, it is noticeably slower and more resource-intensive (particularly for large files) than using gzipped files.In Python 2, opened URLs are not file-like enough for GzipFile or BZ2File to read directly from them, so they must first be loaded entirely into memory. With Python 3, this limitation is not present, so reading remote Gzipped or Bzipped files is significantly cheaper with respect to memory requirements.