Skip to content

Commit dca1508

Browse files
author
steven.chen3
committed
file read and write
1 parent a564f39 commit dca1508

6 files changed

Lines changed: 40 additions & 13 deletions

File tree

helloworld/check_file_39.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
import os.path as ospath
3+
from pathlib import Path
4+
5+
print(ospath.exists("newfile2.txt"))
6+
print(ospath.isfile("newfile.txt"))
7+
print(ospath.isdir("newfile.txt"))
8+
9+
print("===============")
10+
path = Path("sample.txt")
11+
print(path.exists())
12+
print(path.is_file())
13+
print(path.is_dir())
14+
15+
# os.rename("newfile.txt", "newfile2.txt")
16+
os.remove("newfile2.txt")

helloworld/create_file_39.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
file_instance = open("newfile.txt", 'x') # 'w'
2+
file_instance.write("asdfasfdasdf\n")
3+
file_instance.close()

helloworld/main.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +0,0 @@
1-
import sys
2-
3-
4-
def main():
5-
args = sys.argv[1:]
6-
7-
for arg in args:
8-
print(arg)
9-
10-
11-
if __name__ == '__main__':
12-
main()
13-

helloworld/read_file_39.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
file_instance = open("sample.txt", 'r')
2+
3+
while True:
4+
line = file_instance.readline()
5+
if line == '':
6+
break
7+
8+
print(line)
9+
10+
file_instance.close()

helloworld/sample.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This is new string
2+
aaaaa
3+
bbbbbb
4+
cccccc

helloworld/write_file_39.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
file_instance = open("sample.txt", 'w') # 'w' - overwrite
2+
3+
file_instance.write("This is new string\n")
4+
5+
file_instance.writelines(["aaaaa\n", "bbbbbb\n", "cccccc\n"])
6+
7+
file_instance.close()

0 commit comments

Comments
 (0)