-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiles_in_Python.py
196 lines (151 loc) · 5.26 KB
/
Files_in_Python.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File: Files_in_Python.py
# Description: Examples on how to work with files in Python
# Environment: PyCharm and Anaconda environment
#
# MIT License
# Copyright (c) 2018 Valentyn N Sichkar
# github.com/sichkar-valentyn
#
# Reference to:
# [1] Valentyn N Sichkar. Examples on how to work with files in Python // GitHub platform [Electronic resource]. URL: https://github.com/sichkar-valentyn/Working_with_files_in_Python (date of access: XX.XX.XXXX)
# Possible modes to open file with:
# r (read) - open for reading (by default)
# w (write) - open to write, all information in the file will be deleted
# a (append) - open to write, writing will be done at the end
# b (binary) - open in a binary mode
# t (text) - open in the text mode (by default)
# r+ - open for reading and writing
# w+ - open for reading and writing, all information will be deleted
# Opening file for reading
f = open('test.txt', 'r')
# Reading first 5 symbols from the file
x = f.read(5)
print(x)
# Reading file till the end
y = f.read()
print(y)
# Showing the y as the representation of line type
print(repr(y))
# Method to split the information from the file to the different lines
y = y.splitlines()
print(y)
# Closing the file
f.close()
# Opening file for reading
f = open('test.txt', 'r')
# Reading just one line from the file
z = f.readline()
z = z.rstrip() # Deleting the right symbol to move for the next line
print(repr(z))
# Reading the second line
z = f.readline().strip() # Another way how to delete symbol to move for the next line
print(repr(z))
# Closing the file
f.close()
# Opening file for reading
f = open('test.txt', 'r')
# Reading lines one by one using loop
for line in f:
line = line.rstrip()
print(repr(line))
# When everything is read already from the file
# The last try will give the empty string
x = f.read()
print(repr(x))
# Closing the file
f.close()
# Opening file for writing
# If the file doesn't exist, it will be created
f = open('test_w.txt', 'w')
# Writing the line into the file
f.write('Hello!\n') # We use here the \n to move to the next line
f.write('World!\n\n')
# Another way to write lines in the file in separate rows
lines = ['Line1', 'Line2', 'Line3']
content = '\n'.join(lines) # We use .join and say that symbol \n has to be places between lines
f.write(content)
# Closing the file
f.close()
# Opening file for appending
# If the file doesn't exist, it will be created
f = open('test_a.txt', 'a')
f.write('Hello!\n') # Each time we will run this code it will append new info in the file
# Closing the file
f.close()
# Another and recommended way to open file and close it automatically
with open('test.txt') as f:
for line in f:
line = line.rstrip()
print(line)
# Here file will be already closed
# It is possible to open several files at the same time
with open('test.txt') as f, open('test_copy.txt', 'w') as w:
for line in f:
w.write(line) # Copying all info from file f to w
# Implementing the task
# there is a file with some amount of lines
# It is needed to create the copy of this file but with opposite order of the lines in it
with open('test.txt') as f, open('test_copy.txt', 'w') as w:
lst = []
# Reading all the lines and putting them into the list
for line in f:
lst += [line.rstrip()]
# Reordering the elements of the list in reverse direction
lst = lst[::-1]
# Joining all the elements from the list with the symbol \n
content = '\n'.join(lst)
# Writing the information into the copy file
w.write(content)
# Using os and os.path while working with files
import os
import os.path
# Showing all files and directories in the current folder
print(os.listdir())
# Getting the current directory
print(os.getcwd())
# Changing the current directory
os.chdir('tensorflow')
print(os.getcwd())
# Changing back the directory in one level upper
os.chdir('..')
print(os.getcwd())
# Showing the files in the specific directory
print(os.listdir('tensorflow'))
# Checking if the file exists
print(os.path.exists('test.txt'))
print(os.path.exists('tensorflow'))
# Checking if the path is the file
print(os.path.isfile('test.txt'))
print(os.path.isfile('tensorflow'))
# Checking if the file is the directory
print(os.path.isdir('test.txt'))
print(os.path.isdir('tensorflow'))
# Getting the absolute path to the file
print(os.path.abspath('test.txt'))
# Using os.walk for going through all directories and files in them from thr current directory
for current_dir, dirs, files in os.walk('.'): # Fullstop here means the current directory
print(current_dir, dirs, files)
# Using shutil for copying the files and directories
import shutil
# Copying the file
shutil.copy('test.txt', 'test_test.txt')
# Copying the directory
#shutil.copytree('tensorflow', 'tensorflow_copy')
# Implementing the task
# Reading the directory and all sub-directories
# And showing only that directories which contain the .py files
# Also, sorting the results in lexicographical order
lst = []
for current_dir, dirs, files in os.walk('main'): # You can use your own directory or any other
for x in files:
if '.py' in x:
lst += [current_dir]
break
# Sorting the resulted list
lst.sort()
# Preparing data for writing
content = '\n'.join(lst)
# Writing in the file
f = open('results.txt', 'w')
f.write(content)
f.close()