-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
61 lines (53 loc) · 1.31 KB
/
core.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
import os
import sys
import shutil
import datetime
def save_log(message):
log = f'{datetime.datetime.now()} - {message}'
with open('log.txt', 'a', encoding='utf-8') as f:
f.write(log + '\n')
def list_dir(folders_only=False):
if folders_only:
print([item for item in os.listdir() if os.path.isdir(item)])
else:
print(os.listdir())
def create_file(name, text=None):
with open(name, 'w', encoding='utf-8') as f:
if text:
f.write(text)
def create_dir(name):
try:
os.mkdir(name)
except FileExistsError:
print('Directory is already exist')
def remove_f(name, folders_only=False):
if folders_only:
os.rmdir(name)
else:
os.remove(name)
def copy_f(name, new_name):
if os.path.isdir(name):
try:
shutil.copytree(name, new_name)
except FileExistsError:
print('Directory is already exist')
else:
shutil.copy(name, new_name)
def cange_dir(name):
try:
os.chdir(name)
except FileNotFoundError:
print('There is no such path')
if __name__ == '__main__':
create_file('file.txt')
create_file('file.txt', 'some text')
create_dir('dir_1')
list_dir()
list_dir(True)
remove_f('dir_1', True)
remove_f('file.txt')
create_dir('dir_1')
create_file('file.txt')
copy_f('dir_1', 'dir_2')
copy_f('file.txt', 'file1.txt')
save_log('Message')