0% found this document useful (0 votes)
14 views12 pages

Text Files

The document demonstrates various file handling operations in Python like opening, reading, writing, closing files. It shows how to open a file in different modes, write and append content to files. It also discusses various file reading methods like read(), readline(), readlines() and how to iterate over file content line by line or word by word. Various examples count the number of vowels, lines, words in a text file based on certain conditions.

Uploaded by

lanfury45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

Text Files

The document demonstrates various file handling operations in Python like opening, reading, writing, closing files. It shows how to open a file in different modes, write and append content to files. It also discusses various file reading methods like read(), readline(), readlines() and how to iterate over file content line by line or word by word. Various examples count the number of vowels, lines, words in a text file based on certain conditions.

Uploaded by

lanfury45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

In [2]:

1 f = open("[Link]", 'r')

In [3]:

1 [Link]("OK.")

Out[3]:

In [4]:

1 [Link]("\nThis is a line.")

Out[4]:

16

In [5]:

1 [Link]()

In [6]:

1 [Link]("1234")

Out[6]:

In [7]:

1 [Link]()

In [8]:

1 [Link]()

In [9]:

1 f

Out[9]:

<_io.TextIOWrapper name='[Link]' mode='w' encoding='UTF-8'>

In [14]:

1 [Link]

Out[14]:

True
In [15]:

1 print("a\nb")

a
b

In [16]:

1 print("a\\nb")

a\nb

In [17]:

1 print(r"a\nb")

a\nb

In [18]:

1 f = open("[Link]", 'w')

In [19]:

1 [Link]("qwerty")

Out[19]:

In [20]:

1 [Link]()

In [21]:

1 f = open('[Link]', 'a')

In [22]:

1 [Link]("\nparrot")

Out[22]:

In [23]:

1 [Link]()

In [1]:

1 f = open("[Link]")
In [2]:

1 [Link]

Out[2]:

'[Link]'

In [3]:

1 [Link]

Out[3]:

'r'

In [4]:

1 [Link]

Out[4]:

False

In [5]:

1 [Link]("abc")

----------------------------------------------------------------------
-----
UnsupportedOperation Traceback (most recent call
last)
<ipython-input-5-7fbd055c1d19> in <module>
----> 1 [Link]("abc")

UnsupportedOperation: not writable

In [6]:

1 x = 10

In [7]:

1 [Link]()

In [8]:

1 f = open("[Link]", 'a')

In [9]:

1 [Link]("\nToday is Friday.")

Out[9]:

17

In [10]:

1 [Link]()
In [11]:

1 #writelines()

In [12]:

1 l = ["\nRohit", "\nVirat", "\nFridays are awesome."]

In [13]:

1 [Link](l)

In [14]:

1 [Link]()

In [15]:

1 l = ["Chandigarh", " Patiala", "Mansa"]

In [16]:

1 [Link](l)

In [17]:

1 [Link]()

In [18]:

1 [Link]("\n")

Out[18]:

In [19]:

1 [Link](l)

In [20]:

1 [Link]()

In [21]:

1 [Link]()

In [22]:

1 f = open("[Link]") # open("[Link]", 'r')

In [24]:

1 #read(n)
In [25]:

1 a = [Link]()

In [26]:

1 print(a)

qwerty
parrot
Today is Friday.
Rohit
Virat
Fridays are [Link] PatialaMansa
Chandigarh PatialaMansa

In [27]:

1 a

Out[27]:

'qwerty\nparrot\nToday is Friday.\nRohit\nVirat\nFridays are awesome.C


handigarh PatialaMansa\nChandigarh PatialaMansa'

In [28]:

1 [Link]()

Out[28]:

''

In [29]:

1 [Link]()

Out[29]:

''

In [30]:

1 [Link](0)

Out[30]:

In [31]:

1 [Link]()

Out[31]:

'qwerty\nparrot\nToday is Friday.\nRohit\nVirat\nFridays are awesome.C


handigarh PatialaMansa\nChandigarh PatialaMansa'
In [32]:

1 [Link](0)

Out[32]:

In [33]:

1 [Link](5)

Out[33]:

'qwert'

In [34]:

1 [Link](1)

Out[34]:

'y'

In [35]:

1 [Link](1)

Out[35]:

'\n'

In [36]:

1 [Link](6)

Out[36]:

'parrot'

In [37]:

1 [Link]()

Out[37]:

'\nToday is Friday.\nRohit\nVirat\nFridays are [Link] Pati


alaMansa\nChandigarh PatialaMansa'

In [38]:

1 [Link](0)

Out[38]:

0
In [39]:

1 [Link]()

Out[39]:

'qwerty\n'

In [40]:

1 [Link]()

Out[40]:

'parrot\n'

In [41]:

1 [Link]()

Out[41]:

'Today is Friday.\n'

In [42]:

1 [Link]()

Out[42]:

'Rohit\n'

In [43]:

1 [Link]()

Out[43]:

'Virat\n'

In [44]:

1 [Link]()

Out[44]:

'Fridays are [Link] PatialaMansa\n'

In [45]:

1 [Link]()

Out[45]:

'Chandigarh PatialaMansa'

In [46]:

1 [Link]()

Out[46]:

''
In [47]:

1 [Link](0)

Out[47]:

In [48]:

1 [Link]()

Out[48]:

'qwerty\n'

In [49]:

1 [Link]()

Out[49]:

'parrot\n'

In [50]:

1 [Link](2)

Out[50]:

'To'

In [51]:

1 [Link]()

Out[51]:

'day is Friday.\n'

In [52]:

1 [Link]()

Out[52]:

'Rohit\nVirat\nFridays are [Link] PatialaMansa\nChandigarh


PatialaMansa'

In [53]:

1 [Link](0)

Out[53]:

0
In [54]:

1 [Link]()

Out[54]:

['qwerty\n',
'parrot\n',
'Today is Friday.\n',
'Rohit\n',
'Virat\n',
'Fridays are [Link] PatialaMansa\n',
'Chandigarh PatialaMansa']

In [55]:

1 [Link]()

Out[55]:

[]

In [56]:

1 [Link](0)

Out[56]:

In [57]:

1 lines = [Link]()

In [58]:

1 lines

Out[58]:

['qwerty\n',
'parrot\n',
'Today is Friday.\n',
'Rohit\n',
'Virat\n',
'Fridays are [Link] PatialaMansa\n',
'Chandigarh PatialaMansa']
In [59]:

1 for l in lines:
2 print(l)

qwerty

parrot

Today is Friday.

Rohit

Virat

Fridays are [Link] PatialaMansa

Chandigarh PatialaMansa

In [63]:

1 for l in lines:
2 print([Link]())

qwerty
parrot
Today is Friday.
Rohit
Virat
Fridays are [Link] PatialaMansa
Chandigarh PatialaMansa

In [3]:

1 #count number of vowels in a text file.


2 c = 0
3 #1. open file
4 #2. read data into a string
5 #3. iterate over the string
6 f = open("[Link]", 'r')
7 data = [Link]()
8 for ch in data:
9 if [Link]() in 'aeiou':
10 c+=1
11 print("Number of vowels: ",c)
12 [Link]()

Number of vowels: 38
In [5]:

1 #count number of lines starting with T/t


2 c = 0
3 f = open("[Link]", 'r')
4 lines = [Link]()
5 for l in lines:
6 if l[0].upper() == 'T':
7 c+=1
8 print("Number of lines starting woth T - ", c)

Number of lines starting woth T - 3

In [11]:

1 #count number of words starting with T


2 c = 0
3 f = open("[Link]", 'r')
4 data = [Link]()
5 words = [Link]()
6 for w in words:
7 if w[0] == 'T':
8 c+=1
9 print("Number of words starting woth T - ", c)

Number of words starting woth T - 3

In [6]:

1 line = "India is my country"

In [9]:

1 [Link]()

Out[9]:

['India', 'is', 'my', 'country']

In [12]:

1 #count number of spaces in a text file.


2 f = open("[Link]", 'r')
3 data = [Link]()
4 c = [Link](" ")
5 print("Number of spaces: ",c)

Number of spaces: 10

In [14]:

1 #count number of spaces in a text file.


2 f = open("[Link]", 'r')
3 data = [Link]()
4 c = [Link]("\n")
5 print("Number of lines: ",c+1)

Number of lines: 9
In [17]:

1 c = 0
2 f = open("[Link]")
3 words = [Link]().split()
4 c = 0
5 for w in words:
6 if len(w)==3:
7 c+=1
8 print(c)

In [ ]:

You might also like