<jupyter_start><jupyter_text>기본 처리 파일 쓰기<jupyter_code>with open('a.txt','w') as f:
  f.writelines(['hi\n','bye\n'])
  f.write('hello\n')

!cat a.txt<jupyter_output>hi
bye
hello<jupyter_text>파일 읽기<jupyter_code>with open('a.txt','r') as f:
  lines=f.readlines()
  print(lines)<jupyter_output>['hi\n', 'bye\n', 'hello\n']<jupyter_text>with 문으로 파일 open<jupyter_code>with open('/content/sample_data/anscombe.json','r') as f:
  print( f.readlines() )
# f.close()<jupyter_output>['[\n', '  {"Series":"I", "X":10.0, "Y":8.04},\n', '  {"Series":"I", "X":8.0, "Y":6.95},\n', '  {"Series":"I", "X":13.0, "Y":7.58},\n', '  {"Series":"I", "X":9.0, "Y":8.81},\n', '  {"Series":"I", "X":11.0, "Y":8.33},\n', '  {"Series":"I", "X":14.0, "Y":9.96},\n', '  {"Series":"I", "X":6.0, "Y":7.24},\n', '  {"Series":"I", "X":4.0, "Y":4.26},\n', '  {"Series":"I", "X":12.0, "Y":10.84},\n', '  {"Series":"I", "X":7.0, "Y":4.81},\n', '  {"Series":"I", "X":5.0, "Y":5.68},\n', '\n', '  {"Series":"II", "X":10.0, "Y":9.14},\n', '  {"Series":"II", "X":8.0, "Y":8.14},\n', '  {"Series":"II", "X":13.0, "Y":8.74},\n', '  {"Series":"II", "X":9.0, "Y":8.77},\n', '  {"Series":"II", "X":11.0, "Y":9.26},\n', '  {"Series":"II", "X":14.0, "Y":8.10},\n', '  {"Series":"II", "X":6.0, "Y":6.13},\n', '  {"Series":"II", "X":4.0, "Y":3.10},\n', '  {"Series":"II", "X":12.0, "Y":9.13},\n', '  {"Series":"II", "X":7.0, "Y":7.26},\n', '  {"Series":"II", "X":5.0, "Y":4.74},\n', '\n', '  {"Series":"III", "X":10.0, "Y":[...]<jupyter_text>참고사항.- with 를 쓰는 경우 close를 쓰지 않아도 알아서 닫아줍니다.  Os Liberary로 처리<jupyter_code>import os
from os.path import join,isfile,isdir
# dir(os.path)
# 'abspath','altsep','basename','commonpath','commonprefix','curdir','defpath','devnull',
# 'dirname','exists','expanduser','expandvars','extsep','genericpath','getatime','getctime',
# 'getmtime','getsize','isabs','isdir','isfile','islink','ismount','join','lexists','normcase',
# 'normpath','os','pardir','pathsep','realpath','relpath','samefile','sameopenfile','samestat',
# 'sep','split','splitdrive','splitext','stat','supports_unicode_filenames','sys'<jupyter_output><empty_output><jupyter_text>cd,pwd<jupyter_code>currentPath=os.getcwd()

print (os.getcwd()) #현재 디렉토리의

os.chdir("/")

print (os.getcwd()) #현재 디렉토리의


os.chdir(currentPath)
print (os.getcwd()) #현재 디렉토리의<jupyter_output>/Users/nowage/GoogleDrive_finfra/Git/NumpyAndPandas/Numpy
/
/Users/nowage/GoogleDrive_finfra/Git/NumpyAndPandas/Numpy<jupyter_text>join<jupyter_code>a_txt_path=join(currentPath,'a.txt')
a_txt_path<jupyter_output><empty_output><jupyter_text>isfile<jupyter_code>isfile(a_txt_path)<jupyter_output><empty_output><jupyter_text>isdir<jupyter_code>isdir(currentPath)<jupyter_output><empty_output><jupyter_text>ls<jupyter_code>print(os.listdir(os.getcwd()))

# 현재 작업 폴더 얻기,
os.getcwd()
# 특정 경로에 대해 절대 경로 얻기,
os.path.abspath("./Scripts")
# 경로 중 디렉토리명만 얻기,
currentPath=os.getcwd()
a_txt_path=join(currentPath,'a.txt')
os.path.dirname(a_txt_path)

# 파일명을 얻기,
os.path.basename(a_txt_path)
# "파일 각 경로를 나눠 리스트로 리턴하기,
os.path.split(a_txt_path)
# os.path.sep은 OS별 경로 분리자",
a_txt_path.split(os.path.sep)
# 디렉토리 안의 파일/서브디렉토리 리스트,
os.listdir(currentPath)
# 파일 혹은 디렉토리 경로가 존재하는지 체크하기,
os.path.exists(a_txt_path), os.path.exists(currentPath)
# 디렉토리 경로가 존재하는지 체크하기,
os.path.isdir(currentPath)
# 파일 경로가 존재하는지 체크하기,
os.path.isfile(a_txt_path)
# 파일의 크기,
!echo "1234">{a_txt_path}
os.path.getsize(a_txt_path)<jupyter_output><empty_output><jupyter_text>pathlib.Path* Manual :<jupyter_code>from pathlib import Path
parentPath=Path(basePath).parent
# join(os.path.abspath( join(basePath,"..") )
parentPath<jupyter_output><empty_output><jupyter_text>Mkdir<jupyter_code>import os
directory='/tmp/xx/yy'
if not os.path.exists(directory):
    os.makedirs(directory)
!ls /tmp/xx<jupyter_output>[1m[34myy[m[m<jupyter_text>Examples full example<jupyter_code>import os
import sys
from pathlib import Path

basePath=os.path.dirname(sys.argv[0])

childPath=os.path.join(basePath,"test")

os.makedirs(childPath)
print(os.listdir(basePath))
parentPath=Path(basePath).parent
# join(os.path.abspath( join(basePath,"..") )
executedPath=os.getcwd()

os.path.isdir(basePath)

os.path.isfile(basePath)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2574280960304255, 'fraction_numerical': 0.031376277632517234, 'mean_word_length': 7.155038759689923, 'pattern_counts': {'":': 69, '<': 44, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '416577', 'license': 'MIT', 'n_tokens_mistral': 2131, 'n_tokens_neox': 2174, 'n_words': 238, 'path': 'Python/6.PythonFileProcessing.ipynb', 'repo_name': 'Finfra/basicTech', 'size': '4207'}