From wiki
Jump to: navigation, search


Return a list of files matching 'filespec'.

Code example:

import glob
files = glob.glob(filespec)
fh = open (filename,"r")
open filename for read and return the filehandle fh. Use w for write, a for append.
Close the file for filehandle fh.

Code example:

import os
if os.path.isfile(filename):
    f1 =  open (filename,"r")
    for line in f1:
basename = filepath.split('/')[-1]
Get the filename from a path
statinfo = os.stat(filename)
Get file metadata like:
posix.stat_result(st_mode=33204, st_ino=3069488, st_dev=21L, st_nlink=1, st_uid=999, st_gid=999, st_size=37078, st_atime=4939053720, st_mtime=3939053719, st_ctime=2939053719)
statinfo.st_size has the filesize in bytes.
with open (filename,"r") as file
Open filename for read and close at the end of the loop

Code example:

with open (filename,"r") as file:
    for line in file:
Return 'size' bytes from the file as string. If size is omitted or 0 the entire file is returned.
Return all lines from file as list.
Read through all files specified on the commandline.
If there are no files on the commandline read standard input
import fileinput
for line in fileinput.input():
Write line to file opened on filehandle f1
Write to standard output

Zip a file

Check this page.

import zipfile,zlib
zipname = filename+'.zip'
zfile = zipfile.ZipFile(zipname, mode='w')
if zfile:
    zfile.write(filename, compress_type=zipfile.ZIP_DEFLATED)

Read from standard input and keyboard

Read from standard input

import sys
for line in sys.stdin:

Prompt and read from keyboard into a

a = input("Prompt: ")

In python2

a = raw_input("Prompt: ")

Read a csv

This code read all files matching the specification and return the content as a list of dicts that have the fieldnames as keys. Fieldnames must be on the first line of the file an must be unique.

def csv2dict(filespec,seperator=','):
    outfile = []
    filedir = glob.glob(filespec)
    for filename in filedir:
        if os.path.isfile(filename):
            fh = open (filename,"r")
            filelist = list(fh)
            line = filelist.pop(0).rstrip('\r\n')
            header = line.split(seperator)
            fieldnames = set(header)
            if len(header) != len(fieldnames):
                print('ERROR: Fieldnames in '+filename+' are not unique')
                numfields = len(header)
                linecount = 0
                for line in filelist:
                    linecount += 1
                    line = line.rstrip('\r\n')
                    fields = line.split(seperator)
                    linedict = {}
                    count = 0
                    for field in fields: 
                        linedict[header[count]] = field
                        count += 1
                        if count > numfields-1:
                    if count != numfields:
                        print('ERROR: invalid number of fields in line '+str(linecount))                    

Read an Excell file