import glob
glob.glob("/usr/local/*) -- return a list of actual file path
os.listdir()
Code segment to iterate a folder using a callback:
import os
import sys
def func_walker(arg, dirName, itemNames):
for oneItemName in itemNames:
onePath = os.path.join(dirName, oneItemName)
if os.path.isdir(onePath):
print "\t%s/" % oneItemName
else:
print "\t%s" % oneItemName
for one_arg in sys.argv[1:]:
file_path = os.path.abspath(one_arg)
os.path.walk(file_path, func_walker, None);
Code segment to iterate a folder using a for loop:
import os
import sys
for one_arg in sys.argv[1:]:
file_path = os.path.abspath(one_arg)
for thisDir, subDirs, filesHere in os.walk(file_path):
print "thisDir = ", thisDir
print "subDirs = ", subDirs
print "filesHere = ", filesHere
We can retrieve the file path for the script that started the process:
cur_path = sys.path[0]
if (len(cur_path) > 0):
...
The check is needed 'cos it may be an empty string.
To check if a file path is real or not:
os.path.exists(file_path)
Empty a folder content:
import shutil
shutil.rmtree('/path/to/folder')
Create folder(s) following the path
os.makedirs('path/to/new_folder')
Split file name and extension:
theName, theExt = os.path.splitext(oneFile)
theExt will start with '.' or empty string
To output the dir that contains the current running script:
import os
script_dir = os.path.dirname(os.path.realpath(__file__)) + os.sep
Read file line by line:
theList = [];
rfile = open("test-line.txt")
while 1:
line = rfile.readline()
if (line == None) or (0 >= len(line)):
break
# line = line.strip()
print '"' + line + '"'
theList.append(line)
In newer Python, we can do the following:
theFile = open("../readme.txt")
for oneLine in theFile:
print oneLine,
with open(full_path) as the_file:
for one_line in the_file:
...
Read a CSV file:
import sys
import csv
csv.field_size_limit(sys.maxsize)
# rfile = csv.reader(open(target_file_name, 'rb'))
rfile = csv.reader(open(target_file_name, 'r'))
for row in rfile:
print row[0]
or
rfile = csv.DictReader(open(target_file_name, 'rU'))
print rfile.fieldnames # the first row normally
for row in rfile:
print row[rfile.fieldnames[0]] # first field name, may not be the first one in the list
Write a list to CSV file:
import csv
wfile = open(r'test-line.csv','wb')
w = csv.writer(wfile, dialect='excel', quoting=csv.QUOTE_ALL)
w.writerow(theList)
Or
data1 = { 'apple': 1, 'banana': 2, 'orange': 3, }
data2 = {'apple': 11, 'banana': 21, 'orange': 31, }
field_order2 = ['apple', 'banana', 'orange']
csv_writer = csv.DictWriter(open('test-20130610.csv', 'wb'), fieldnames = field_order2, dialect='excel')
csv_writer.writeheader()
csv_writer.writerow(data1)
csv_writer.writerow(data2)
Image Header:
import imghdr
imageType = imghdr.what(srcFullPath)
Copy File
import shutil
shutil.copy2(srcFullPath, finalDstPath)
Zip File
import zipfile
zipper = zipfile.ZipFile(zipFilePath, 'w')
for thisDir, subDirs, filesHere in os.walk(absPath):
for oneFile in filesHere:
# this will create a flat file structure
# take out second param ('oneFile') will create a hierarchical structure from the path in the first parameter
zipper.write(os.path.join(thisDir, oneFile), oneFile)
Read File
theFile = open(file_path, 'rb')
buf = theFile.read()
if (buf != None):
...
theFile.close()
Write File
theFile = open(thePath, 'wb')
strToFile = ...
theFile.write(strToFile)
theFile.close()
Links:
A python replacement for java.util.Properties (Python recipe)