Last active
December 17, 2015 05:09
-
-
Save kvu787/5555751 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import glob | |
class Directory(object): | |
"""Creates wrapper object for a directory. | |
Attributes: | |
path: str, relative path to directory. | |
""" | |
def __init__(self, path = '.'): | |
"""Creates wrapper class for a valid directory. | |
Args: | |
path: Path to directory. Defaults to current directory. | |
Returns: | |
None | |
Raises: | |
Exception: If directory path is invalid. | |
""" | |
if not os.path.isdir(path): | |
raise Exception("Invalid directory path.") | |
self.path = os.path.normpath(path) | |
def file_names(self): | |
"""List names of files (excludes directories). | |
Returns: | |
Sorted (ascending) array of file names | |
""" | |
all_items = glob.glob(self.path + '/*') | |
files_only = [os.path.basename(f) for f in all_items if os.path.isfile(f)] | |
files_only.sort() | |
return files_only | |
def directory_names(self): | |
all_items = glob.glob(self.path + '/*') | |
directories_only = [os.path.basename(f) for f in all_items if os.path.isdir(f)] | |
directories_only.sort() | |
return directories_only | |
class File(object): | |
"""Creates wrapper object for a file. | |
Attributes: | |
path: str, relative path to file. | |
""" | |
def __init__(self, path): | |
"""Creates wrapper class for a valid file. | |
Args: | |
path: Path to file. | |
Returns: | |
None | |
Raises: | |
Exception: If file path is invalid. | |
""" | |
if not os.path.isfile(path): # check isfile during runtime? | |
raise Exception("Invalid file path.") | |
self.path = path | |
def read(self): | |
"""Reads content of file. | |
Returns: | |
String of file data. | |
""" | |
f = open(self.path, 'r') | |
content = f.read() | |
f.close() | |
return content | |
def write(self, data): | |
f = open(self.path, 'w') | |
f.writelines(data) | |
f.close() | |
def append_to_line(self, line_num, chars): | |
"""Writes content to the end of a specified line in the file. | |
Args: | |
line_num: int, line number in file (first line is 0). | |
chars: str appended to line. | |
Returns: | |
None | |
Raises: | |
IndexError: If line_num is out of file range. | |
""" | |
f = open(self.path, 'r') | |
data = f.readlines() | |
try: | |
data[line_num] = data[line_num].rstrip('\n') + chars + '\n' | |
except IndexError: | |
raise IndexError("Line number is out of range.") | |
finally: | |
f.close() | |
f.close() | |
self.write(data) | |
class CSVFile(File): | |
def parse(self): | |
data = self.read() | |
data = [line.split(',') for line in data.split('\n')] | |
if data[-1] == ['']: | |
data.pop() | |
return data | |
def get_cell(self, row, col): | |
return self.parse()[row][col] | |
def write_cell(self, row, col, data): | |
csv = self.parse() | |
csv[row][col] = str(data) | |
string = '\n'.join([','.join(row) for row in csv]) | |
self.write(string) | |
def get_timestamps(directory): | |
return [file_name[7:13] for file_name in directory.file_names()] | |
## MAIN METHOD | |
if __name__ == "__main__": | |
timestamp_root = "1824/" | |
root_directory = Directory(timestamp_root) | |
timestamp_directories = root_directory.directory_names() | |
current_timestamp_directory = Directory(timestamp_root + timestamp_directories[0]) | |
timestamps = get_timestamps(current_timestamp_directory) | |
f = CSVFile('1824.csv') | |
print f.parse()[0] | |
for i in range(1, len(f.parse())): | |
if len(timestamps) == 0: | |
print "Moving to new directory..." | |
timestamp_directories.pop(0) | |
timestamps = get_timestamps(Directory(timestamp_root + timestamp_directories[0])) | |
f.write_cell(i, 16, timestamps[0]) | |
print "Wrote " + str(timestamps[0]) + " to row " + str(i + 1) | |
if f.parse()[i][15] == 'w': | |
timestamps.pop(0) | |
print 'check on row', i | |
print "done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment