Repository navigation
Expand file tree
/
Copy pathReadWriteFile.py
More file actions
51 lines (36 loc) · 1.5 KB
/
Copy pathReadWriteFile.py
File metadata and controls
51 lines (36 loc) · 1.5 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
# Demonstrates basic file operations in python
# - Reads a text file and writes content of file to stdout
# - Reads a text file and writes content of file into an output file
#
# Created By: Josh Johnson
from os import path # used to get relative path for text files
# get current directory location of ReadWriteFile.py
base_path = path.dirname(__file__)
# *** reads a text file and writes content of file to stdout ***
# relative path to the test file
file_path = path.abspath(path.join(base_path, "..", "Test_Files", "basic_text.txt"))
# open access to test file
file1 = open(file_path, "r")
# reading from variable storing access to the test file and saving its content
data = file1.read()
# closing the file access to test file
file1.close()
# printing data stored from reading file to stdout
print(data)
# *** reads a text file and writes content of file into an output file ***
# relative path to the test file
file_path = path.abspath(path.join(base_path, "..", "Test_Files", "poem.txt"))
# open access to test file
file1 = open(file_path, "r")
# reading from variable storing access to the test file and saving its content
data = file1.read()
# closing the file access to test file
file1.close()
# relative path to the output file
file_path = path.abspath(path.join(base_path, "Output_Files", "poem_dup_from_readwritefile.txt"))
# creates output file access
file2 = open(file_path, "w")
# write content from test file to output file
file2.write(data)
# closing the file access to the output file
file2.close()