-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile_Handling.py
More file actions
138 lines (96 loc) · 3.8 KB
/
File_Handling.py
File metadata and controls
138 lines (96 loc) · 3.8 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Pytohn has several functions for creating, reading, updating and deleting files.
# File Handling
'''
--> The key function for working with files in python is the 'open()' function.
--> The open() function takes two parameters; filename and mode.
--> There are four different method(modes) for opening a file.
--> "r" - READ - Default value. opens a file for readng, error if the file does not exist.
--> "a" - Append - opens a file for appending, creates the file if it does not exist.
--> "w" - Write - opens a file for writing, creates the file if ot does not exist.
--> "x" - create - creates the specified file, returns an error if the file is already exists.
# In addition you can specify if the file should be handled as binary or text mode.
--> "t" - TEXT - default value. Text mode.
--> "b" - Binary mode(e.g. image).
'''
# # syntax:
# NOTE: Make sure the file exists, or else you will get an Error
# # To open a file for reading it is enough to specify the name of the file:
# f = open("demofile.txt")
# # The code ablove is the same as:
# # "r" for read and "t" for text are the default values, you don't need to specify them
# f = open("demofile.txt","rt")
# # Open a file on the server
# # demofile.text
'''Hello.! welcome to demo file
This file is for testing purpose
Good luck!
'''
# # The open() function returns a file object, which has a read() method for reading the content of the file.
# f = open("demofile.txt",'r')
# print(f.read())
# # If the file id located in a different locaiton, you will have to specify the file path, like this.
# # RAW STRNG (r)
# f = open(r"C:\Users\heman\OneDrive\Desktop\welcome.txt.txt",'r')
# print(f.read())
# # DOULBE BACKSLASHES(\\)
# f = open("C:\\Users\\heman\\OneDrive\\Desktop\\welcome.txt.txt",'r')
# print(f.read())
# # FORWARD SLASHES (/)
# f = open(r"C:/Users/heman/OneDrive/Desktop/welcome.txt.txt",'r')
# print(f.read())
# # NOTE : Always remember to close the file after you're done reading fron it using f.close() to free up system resources.
# f.close()
# f = open("demofile.txt",'r')
# # # return the 5 first characters of the file.
# # print(f.read(5))
# # Read Lines
# print(f.readline())
# print(f.readlines())
# f = open("demofile.txt","r")
# for x in f:
# print(x)
# f.close()
# Python file Write
# write to an existing file
# To write to an existing file, you must add a parameter to the open() function.
'''
--> "a" -Append - will append to the end of the file.
--> "w" -write - will overwrite any existing content.
'''
# f = open("demofile.txt","a")
# f.write("\nNow the file has more content")
# f.close()
# f = open("demofile.txt","r")
# print(f.read())
# f = open(r"C:\Users\heman\OneDrive\Desktop\welcome.txt.txt","w")
# f.write("woops.!I have deleted the content! and rewrite the file content ")
# f.close()
# f = open(r"C:\Users\heman\OneDrive\Desktop\welcome.txt.txt","r")
# print(f.read())
# f.close()
# # create a new file
# f = open("demo.txt","x")
# f.close()
# # write the file if the file exist it will overwrite file ....if file does not exist it will create file
# f = open("demo2.txt","w")
# f.write("This is new file created using function and write the file")
# f = open("demo2.txt","r")
# print(f.read())
# f.close()
# f = open("demo2.txt","a")
# f.write("\tadding the new lines to file ")
# f = open("demo2.txt","r")
# print(f.read())
# f.close()
# # python Delete file
# # To delete a file,you must import the OS module, and run it's os.remove() function.
# import os
# os.remove("demo.txt")
# # check if File exist:
# import os
# if os.path.exists(r"C:\Users\heman\OneDrive\Desktop\welcome.txt.txt"):
# os.remove(r"C:\Users\heman\OneDrive\Desktop\welcome.txt.txt")
# else:
# print("The file does not exist")
# import os
# os.rmdir("mytextfile")