-
Notifications
You must be signed in to change notification settings - Fork 1
/
strip_path.py
executable file
·35 lines (28 loc) · 1.23 KB
/
strip_path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import collections, os
def path_elements(path):
head, tail = os.path.split(path)
if head and head != "/":
return path_elements(head) + [tail]
else:
return [tail]
def trim_path_elements(path):
return os.path.join("/", *[element.strip() for element in path_elements(path)])
def fixup_file(input_filename):
values = []
with open(input_filename, "r") as f:
firstline = f.readline()
TupleType = collections.namedtuple("{base}_Tuple".format(base=os.path.splitext(os.path.basename(input_filename))[0]),
firstline.strip().split("|"))
if hasattr(TupleType, "PATH"):
print "fixing path for file '{file}'".format(file=input_filename)
with open(input_filename+"_fix", "w") as w:
w.write(firstline)
for line in f.readlines():
data = TupleType(*line.strip().split("|"))
modifiable = data._asdict()
modifiable["PATH"] = trim_path_elements(data.PATH)
w.write("|".join(TupleType(**modifiable))+"\n")
fixup_file("src_cpy/MEDIA_FILE.dsv")
fixup_file("src_cpy/USER_RATING.dsv")