Skip to content

Commit cbcc6ea

Browse files
author
akashgiricse
committed
Added help material for Reading and writing files using python3
1 parent 5724864 commit cbcc6ea

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#String with a file path
2+
import os
3+
os.path.join('usr', 'bin', 'spam')
4+
# prints string i.e. 'usr/bin/spam'
5+
6+
# join names from a list of filenames to the end of a folder's name:
7+
myFiles = ['account.txt', 'details.csv', 'invite.docx']
8+
for filename in myFiles:
9+
print(os.path.join('rango/Pictures', filename))
10+
#prints string i.e.
11+
# rango/Pictures/account.txt
12+
# rango/Pictures/details.csv
13+
# rango/Pictures/invite.docx
14+
15+
16+
# current working directory
17+
print(os.getcwd())
18+
# prints current directory, in my case it's '/home/rango/ScriptsUsingPython/HelpMaterial'
19+
os.chdir('/home/rango/Pictures')
20+
print(os.getcwd()) # prints /home/rango/Pictures
21+
"""
22+
# Creating New Folder with os.makedirs()
23+
os.makedirs('/home/rango/directory1/subdir/subsubdir/mail.txt')
24+
# will create directory "/home/rango/directory1/subdir/subsubdir/mail.txt"
25+
"""
26+
27+
# Handling Absolute and Relative Paths
28+
os.path.abspath('.')
29+
# returns '/home/rango/ScriptsUsingPython/HelpMaterial'
30+
31+
os.path.abspath('./Pictures')
32+
# returns '/home/rango/Pictures'
33+
34+
os.path.isabs('.')
35+
# returns False
36+
37+
os.path.isabs(os.path.abspath('.'))
38+
# returns True
39+
40+
os.path.relpath('/home/rango', '/home')
41+
# returns 'rango'
42+
43+
os.path.relpath('/home/rango', '/home/rango/Desktop/Books')
44+
# returns '../..'
45+
46+
filePath = '/home/rango/Desktop/Books'
47+
os.path.dirname(filePath)
48+
# returns '/home/rango/Desktop'
49+
os.path.basename(filePath)
50+
# returns 'Books'
51+
os.path.split(filePath)
52+
# returns ('/home/rango/Desktop', 'Books') i.e. both dirname and basename.
53+
filePath.split(os.path.sep)
54+
# returns ['', 'home', 'rango', 'Desktop', 'Books']
55+
'/rango/bin'.split(os.path.sep)
56+
# returns ['', 'rango', 'bin']
57+
58+
# Finding File Sizes and Folder Contenets
59+
os.path.getsize('/home/rango/Desktop/Books/automate-the-boring-stuff-with-python-2015-.pdf')
60+
# returns 17449893 in bytes
61+
os.listdir('/home/rango/Desktop/Books')
62+
# returns ['DjangoBook', 'automate-the-boring-stuff-with-python-2015-.pdf']
63+
64+
totalSize = 0
65+
for filename in os.listdir('/home/rango/Desktop/Books'):
66+
totalSize = totalSize + os.path.getsize(os.path.join('/home/rango/Desktop/Books', filename))
67+
print(totalSize) # prints 17453989
68+
69+
# Checking Path Valitidy
70+
os.path.exists('/home/rango')
71+
True
72+
os.path.exists('/home/rangodango')
73+
False
74+
os.path.isfile('/home/rango/Desktop')
75+
False
76+
os.path.isfile('/home/rango/Desktop/StartDjango.txt')
77+
True
78+
os.path.exists('/media/rango/Windows')
79+
False # when unmounted
80+
os.path.exists('/media/rango/Windows')
81+
True # when mounted
82+
83+
# File Reading/Writing Process
84+
# Opening Files with the open() function
85+
helloFile = open('/home/rango/hello.txt') # stores the file object in Read Mode
86+
helloContent = helloFile.read()
87+
print(helloContent) # prints content inside the hello.txt file
88+
89+
# writing to files
90+
# in python intetective shell
91+
>>> import os
92+
>>> helloFile = open('hello.txt','w') # open hello.txt in write mode i.e. using 'w'
93+
>>> helloFile.write('entered into shell \n')
94+
20
95+
>>> helloFile.close()
96+
>>> helloFile = open('hello.txt', 'a') # open fine in append mode so that it will append text and not replace
97+
>>> helloFile.write('Hello again.')
98+
12
99+
>>> helloFile.close()
100+
>>> helloFile = open('hello.txt')
101+
>>> content = helloFile.read()
102+
>>> helloFile.close()
103+
>>> print(content)
104+
entered into shell
105+
Hello again.
106+
>>>
107+
108+
109+
110+
111+
112+

0 commit comments

Comments
 (0)