Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- for root, dirs, files in os.walk ('A:/pycharm/study0722/data') :
- for f in files :
- print(f)
- # ---------- 파일 목록 읽기 -------------
- os.chdir('A:/pycharm/study0722/data')
- dirextory = ('A:/pycharm/study0722/data')
- for file in dirextory :
- with open(file, 'r') as g :
- read_file = open_file.read()
- print(g)
- # ------------ 여러파일 수정 실패 -----------------
- import glob
- list = glob.glob('A:/pycharm/study0722/data./*.txt')
- for fn in list :
- with open(fn, 'r') as f:
- with open(fn.replace('txt', 'csv'), 'w') as f2 :
- for line in f:
- f2.write(line)
- #----------------- 여러 파일 저장 (확인). 근데 내용이 아니라 파일명만 들어감.. ------------------
- import glob
- list = glob.glob('A:/pycharm/study0722/data./*.txt')
- for fn in list :
- with open(fn, 'r') as f:
- with open(fn.replace('txt', 'csv'), 'w') as f2 :
- for line in f:
- tokens = line.split()
- year = int(tokens[0])
- month = int(tokens[1])
- day = int(tokens[2])
- tmax = float(tokens[3])
- tavg = float(tokens[4])
- tmin = float(tokens[5])
- rainfall = float(tokens[10])
- f2.write("{:d}, {:d}, {:d}, {:.1f}, {:.1f}, {:.1f}, {:.1f}".format(year, month, day, tmax, tavg, tmin, rainfall))
- f2.write(line)
- # -----------여러 파일을 수정한 후 저장 하기 1 실패-----------------------
- import os
- output_dir = 'A:/pycharm/study0722/data'
- for path, dirs, files in os.walk ('A:/pycharm/study0722/data') :
- for f in files:
- with open(os.join('A:/pycharm/study0722/data/',f), 'r') as fr:
- with open(os.path.join(output_dir,f),'w') as fo:
- fo.write(f)
- #---------------- 여러 파일 저장하기 2 실패 --------------
- #----------os.join이 없음.. (?)--------------
Add Comment
Please, Sign In to add comment