with open('{파일경로}', 'a') as f:
f.write('\nwith 문을 사용하면 리스스 해제가 가동으로 된다')
f.write('\nwith 문 블럭 내에서는 연속적인 쓰기가 가능함')
f = open('{파일경로}', 'a')
f.write('\n파일 쓰기가 종료되면 파일쓰기를 반드시 종료해야함')
f.close()
f = open('{파일경로}', 'r')
while True:
tmp = f.readline()
if not tmp:
break
f.close()
f = open('{파일경로}', 'r')
tmp = f.readlines()
for lineResult in tmp:
fileResult += lineResult
f.close()
f = open('{파일경로}', 'r')
tmp_list = list(enumerate(f))
for i in range(len(tmp_list)):
fileResult += tmp_list[i]
f.close()
# readLines 이용
f = open('{파일경로}', 'r', encoding='UTF8' )
print("파일 길이 : %d" % len(f.readlines()))
# enumerate 이용
f = open('{파일경로}', 'r', encoding='UTF8' )
print("파일 길이 : %d" % (len(list(enumerate(f)))))
import os
filepath = "/test/A/B.txt"
# 파일 존재 유무 확인
if not os.path.exists(filepath):
#디렉토리 존재 확인
if not os.path.exists(os.path.dirname(filepath)):
# 폴더가 이미 있는경우 makedirs하면 오류나기 때문에 exist_ok 옵션을 True로 주어 처리
os.makedirs(os.path.dirname(filepath), exist_ok=True)
# 디렉토리 여부 확인
if not os.path.isdir(filepath):
# 파일 작성 시 인코딩 지정
f = open(filepath, 'w', encoding='utf-8')
f.write("test")
f.close()
import os
current_path = os.getcwd()
print("현재 위치 : " + current_path)
import os
current_path = os.path.basename(filename)
import os
current_path = os.path.dirname(filename)
import os
current_path = os.path.split(filename)
import os
current_path = os.path.splitdrive(filename)
import os
current_path = os.path.splitext(filename)
txt = "1열 테스트\n2열 테스트"
x = txt.splitlines()
print(x)
# 결과 = ['1열 테스트', '2열 테스트']
댓글남기기