-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandling.py
More file actions
39 lines (32 loc) · 1.61 KB
/
FileHandling.py
File metadata and controls
39 lines (32 loc) · 1.61 KB
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
# Openfile #
#myFile = open(r"D:\Python Basics\mostafa.txt","a")
#Moods:
#[1] "r" ---> Read (Error if not exist)
#[2] "a" ---> Append values (Create file if not exist)
#[3] "w" ---> Writing (Create file if not exist)
#[4] "x" ---> create file (Error if exist)
#------------------------------------------------------#
# Read file #
#print(myFile) #---> Print info about file object
#print(myFile.read()) #---> read all file
#print(myFile.read(5)) #---> first 5 char of the file
#print(myFile.readline()) #---> frist line
#print(myFile.readline(3)) #---> frist 3 char of the next line
#print(myFile.readline()) #---> complete the line
#print(myFile.readlines()) #---> read all lines as a list
#print(myFile.readlines(5)) #---> Maximum bytes
#-------------------------------------------------------#
# Write #
#myFile.write("Hello sasa") #---> delete all file content and write
#L = ["Mostafa\n","Mohamed\n","Ragab\n"]
#myFile.writelines(L) #---> delete all file content and Write a list
# write ( append mode ) #
#myFile.write("Good morning\n") #---> add this text on the same content
#-------------------------------------------------------#
# Addition info (Append mode) #
#myFile.truncate(5) #---> keep first 5 chars and delete the rest
#myFile.tell() #---> position of cursor
#myFile.seek(6) #---> move cursor to this position
#myFile.close() #---> close the file
#import os
#os.remove(r"D:\Python Basics\mostafa.txt") #---> remove the file