Skip to content

Commit 01134dc

Browse files
committed
Python Course
1 parent f40b0c9 commit 01134dc

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

File.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
# with file I/O read file name "myInfo.txt" how?
22
#let see example
33

4-
data=open("myInfo.txt",'r') # "r" work as read command for file.
4+
#data=open("myInfo.txt",'r') # "r" work as read command for file.
5+
#one another way to read file
6+
data=open("myInfo.txt") #like with this syntan it will take read command by default.
57
info=data.read()
6-
78
print(info)
8-
data.close()# here we are asking to computer to after read this file just close thia function
9+
data.close()# here we are asking to computer to after read this file just close thia function
10+
11+
# if you want to read only limted chractors from file
12+
13+
limit=open('myInfo.txt')
14+
prt=limit.read(4) # this will print only "name" word
15+
print(prt) #you got only Four word from file.
16+
limit.close()
17+
18+
# Read only first line ".readline()"
19+
line=open("myInfo.txt")
20+
printLine=line.readline() #this print only first line of file
21+
print(printLine)
22+
# for sencond line you have to write this function again
23+
printLine=line.readline() #this print only seconed line of file
24+
print(printLine)
25+
# for third line you have to write this function again
26+
printLine=line.readline() #this print only first line of file
27+
print(printLine)
28+
line.close()

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,4 +489,34 @@
489489
```
490490
open("file_name","command")
491491
```
492-
**command can be - read, write, update and delete**
492+
**command can be - read, write, update and delete**
493+
494+
2. Functions to "read" a file
495+
1. ".read()" to read all content.
496+
```
497+
data=open("file_name","r")
498+
info=data.read()
499+
print(info)
500+
data.close()
501+
```
502+
or
503+
```
504+
data=open("file_name")
505+
info=data.read()
506+
print(info)
507+
data.close()
508+
```
509+
2. ".read(value)" value for how much words you want to read
510+
```
511+
data=open("file_name")
512+
info=data.read(5)
513+
print(info)
514+
data.close()
515+
```
516+
3. ".readline()" function will print every time one new line
517+
```
518+
file=open("myInfo.txt")
519+
letRead=file.readline()
520+
print(letRead)
521+
file.close()
522+
```

myInfo.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
name:varinder singh
1+
name: Varinder singh
2+
address: Punjab,India
3+
city: Nabha

0 commit comments

Comments
 (0)