-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_swap.py
85 lines (63 loc) · 2.24 KB
/
file_swap.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'''a work script to rename files from muckrock's convention to something
usable in Dedoose
by @peteyreplies
'''
##import libraries
import os #to walk dirs
import shutil #to copy command
##where are the files, & where do they need to be?
base_dir = '../../../Desktop/scratch/GeoCen/old_docs'
new_dir = '../../../Desktop/scratch/GeoCen/renamed_docs'
##define some functions
def return_contents(dir_path):
'''takes a string of a filepath and returns a list of its contents'''
contents = []
for o in os.listdir(dir_path):
contents.append(o)
for n in contents:
if '.DS_Store' in n:
contents.remove(n)
#print 'walking ' + dir_path
return contents
def check_incoming(response_path):
'''checks if a document is incoming by reading first line for outgoing message
if it is incoming, return true; else, false'''
f = open(response_path)
lines = f.read().splitlines()
try:
if lines[0] == '':
del lines[0]
#print 'checking ' + response_path + ' for incoming'
return 'To Whom' not in lines[0]
except IndexError:
pass
def copy_file(response_path, entity_name, r):
'''takes 3 strings: file dir, entity name, and filename, prepends
entity name to file name, and then copies to renamed folder'''
new_location = new_dir + '/' + entity_name + '_' + r
shutil.copy(response_path,new_location)
#print entity_name + '_' + r + ' copied successfully'
##main loop
#find each entity within each category
i = 0
responding_entities = return_contents(base_dir)
for e in responding_entities:
#get the name of the entity
entity_dir = base_dir + '/' + e
entity_name = e[7:-1]
#debug statement
print 'working within entity ' + entity_name
#find each response within each entity
entity_responses = return_contents(entity_dir)
for r in entity_responses:
response_path = entity_dir + '/' + r
#debug statement
i = i + 1
print 'working on document ' + str(i) + ': ' + entity_name + '_' + r
#if it's not a text file, copy it, saving just the title of the doc
if 'txt' not in r[-3:]:
copy_file(response_path, entity_name, r.split()[-1])
#if it's an incoming txt file, copy it
elif check_incoming(response_path):
copy_file(response_path, entity_name, r)
print 'finished processing ' + str(i) + ' documents'