-
Notifications
You must be signed in to change notification settings - Fork 19
/
file_manage.py
37 lines (30 loc) · 1.36 KB
/
file_manage.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
import os
def create_project_folder(dir): # Create seperate folder for each website
if not os.path.exists(dir):
print('Creating directory ' + dir)
os.makedirs(dir)
def create_data_files(folder_name, start_link): # Append to queue and crawled list
queue = os.path.join(folder_name , 'queue.txt')
data_crawled = os.path.join(folder_name,"crawled.txt")
if not os.path.isfile(queue):
write_to_file(queue, start_link)
if not os.path.isfile(data_crawled):
write_to_file(data_crawled, '')
def write_to_file(path, url): # Create a new file for the task
with open(path, 'w') as f:
f.write(url)
def append_file(path, url): # Append new data to existing file
with open(path, 'a') as file:
file.write(url + '\n')
def empty_queue(path): # Delete contents of a file
open(path, 'w').close()
def convert_to_set(file_name): # Read a file and convert each line to set items
results = set()
with open(file_name, 'rt') as f:
for line in f:
results.add(line.replace('\n', ''))
return results
def set_to_file(urls, file_name): # Iterate through a set, each item will be a line in a file
with open(file_name,"w") as f:
for l in sorted(urls):
f.write(l+"\n")