File tree Expand file tree Collapse file tree 3 files changed +57
-5
lines changed Expand file tree Collapse file tree 3 files changed +57
-5
lines changed Original file line number Diff line number Diff line change 1
1
# with file I/O read file name "myInfo.txt" how?
2
2
#let see example
3
3
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.
5
7
info = data .read ()
6
-
7
8
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 ()
Original file line number Diff line number Diff line change 489
489
```
490
490
open("file_name","command")
491
491
```
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
+ ```
Original file line number Diff line number Diff line change 1
- name:varinder singh
1
+ name: Varinder singh
2
+ address: Punjab,India
3
+ city: Nabha
You can’t perform that action at this time.
0 commit comments