file = open('/Users/min/Documents/pythonEx/project/PythonTxt/text.txt', 'w')
strCnt = file.write('Hello world!')
print(f'strCnt: {strCnt}')
file.close()
file = open('/Users/min/Documents/pythonEx/project/PythonTxt/3_035_exam.txt', 'r')
str = file.read()
print(f'str : {str}')
file.close()
File Mode | 의미 |
---|---|
' w ' | 쓰기 전용(파일이 있으면 덮어씌움) |
' a ' | 쓰기 전용(파일이 있으면 덧붙임) |
' x ' | 쓰기 전용(파일이 있으면 에러 발생) |
' r ' | 읽기 전용(파일이 없으면 에러 발생) |
uri = '/Users/min/Documents/pythonEx/project/PythonTxt/'
with open(uri + '3_037_txtexam.txt', 'a') as f:
f.write('python study!')
languages = ['c/c++', 'java', 'c#', 'python', 'javascript']
uri = '/Users/min/Documents/pythonEx/project/PythonTxt/'
with open(uri + '5_038_writelines_exam.txt', 'a') as f:
f.writelines(item + '\n' for item in languages)
uri = '/Users/min/Documents/pythonEx/project/PythonTxt/'
with open(uri + '5_038_writelines_exam.txt', 'r') as f:
lanList = f.readlines()
print(f'lanList : {lanList}')
print(f'lanList type : {type(lanList)}')
uri = '/Users/min/Documents/pythonEx/project/PythonTxt/'
with open(uri + '5_038_writelines_exam.txt', 'r') as f:
line = f.readline()
while line != '':
print(f'line : {line}', end='')
line = f.readline()