-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathos_module.py
More file actions
101 lines (73 loc) · 4.01 KB
/
os_module.py
File metadata and controls
101 lines (73 loc) · 4.01 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
from datetime import datetime
# Getting the current working directory
print(os.getcwd())
# Changing the current working directory
# os.chdir('/Users/Vinay Pathak/Desktop/')
# print(os.getcwd())
#-------------------------------------------------------------------------------------------------------#
# Listing all the files and folders of the current working directory
# print(os.listdir())
# Creating new folder in current working directory
# os.mkdir('OS_Module_Dir1/Sub-Dir-1') # This will throw an error because the parent diretory OS_Module_Dir1 does not exists
# os.rmdir('OS_Module_Dir1') # To remove the directory
# os.makedirs('OS_Module_Dir2/Sub-Dir-2')# This will create the sub-level directory if the parent directory does not exists even
# os.removedirs('OS_Module_Dir2/Sub-Dir-1') # To remove the subb level directory
# changes in master branch
#-------------------------------------------------------------------------------------------------------#
# ROUGH WORK
path = '/Users/Vinay Pathak/Desktop/python' + r'\class.mkv'
print(path.replace('\\', '/'))
a, b = os.path.split(path.replace('\\', '/'))
print(a)
print(b)
# os.makedirs('VLC File')
# os.chdir(a[1:] + '/' + 'Text File')
# file_path = os.path.join('Text File', b)
# open(os.path.join('VLC File', b), 'wb').close()
if(not os.path.exists(a + '/VLC File')):
os.makedirs('VLC File')
os.rename('/Users/Vinay Pathak/Desktop/python/class.mkv', a + '/VLC File/' + b) # Moving The File from python to VLC File
os.replace('/Users/Vinay Pathak/Desktop/python/class.mkv', a + '/VLC File/' + b) # Moving the File from python to VLC Filr
#-------------------------------------------------------------------------------------------------------#
# Renaming the file name
# os.rename('log.txt', 'log_file.txt')
#-------------------------------------------------------------------------------------------------------#
# Getting the status of the file or directory
# print(os.stat('test.py'))
# mod_time = os.stat('log_file.txt').st_mtime
# print(datetime.fromtimestamp(mod_time))
#-------------------------------------------------------------------------------------------------------#
# TRAVERSING THE DIRECTORY TREE
# for dirpath, dirnames, filenames in os.walk('/Users/Vinay Pathak/Desktop/'):
# print('Current Path:', dirpath)
# print('Directories:', dirnames)
# print('Files:', filenames)
# print()
#-------------------------------------------------------------------------------------------------------#
# print(os.environ.get('HOME'))
# file_path = os.path.join(os.environ.get('HOME'),'test.txt')
# print(file_path)
#-------------------------------------------------------------------------------------------------------#
# basename is the file name at the end of the path
# print(os.path.basename('/Users/Vinay Pathak/Desktop/log_file.txt'))
# If we want to get the directory name at the end of the path
# print(os.path.dirname('/Users/Vinay Pathak/Desktop/log_file.txt'))
# If to get both the file name and the directory name
# print(os.path.split('/Users/Vinay Pathak/Desktop/log_file.txt'))
# a, b = os.path.split('/Users/Vinay Pathak/Desktop/log_file.txt')
# print(a[1:])
# print(b)
#-------------------------------------------------------------------------------------------------------#
# SOME OTHER USEFUL METHODS
# print(os.path.exists('/Users/Vinay Pathak/Desktop/log_file.txt')) # Likewise we can check for directory also
# print(os.path.isdir('/Users/Vinay Pathak/Desktop/'))
# print(os.path.isfile('/Users/Vinay Pathak/Desktop/log_file.txt'))
# Parsing out the extension and the file name
# print(os.path.splitext('/Users/Vinay Pathak/Desktop/log_file.txt'))
# directory, file_ext = os.path.splitext('/Users/Vinay Pathak/Desktop/log_file.txt')
# print(directory)
# print(file_ext)
#-------------------------------------------------------------------------------------------------------#
# TO CHECK THE METHOD AND ATTRIBUTES PRESENT IN OS MODULE
# print(dir(os.path))