Guest User

Untitled

a guest
Jul 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import os
  2.  
  3. for root, dirs, files in os.walk ('A:/pycharm/study0722/data') :
  4. for f in files :
  5. print(f)
  6. # ---------- 파일 목록 읽기 -------------
  7.  
  8. os.chdir('A:/pycharm/study0722/data')
  9. dirextory = ('A:/pycharm/study0722/data')
  10. for file in dirextory :
  11. with open(file, 'r') as g :
  12. read_file = open_file.read()
  13. print(g)
  14.  
  15. # ------------ 여러파일 수정 실패 -----------------
  16.  
  17. import glob
  18.  
  19. list = glob.glob('A:/pycharm/study0722/data./*.txt')
  20. for fn in list :
  21. with open(fn, 'r') as f:
  22. with open(fn.replace('txt', 'csv'), 'w') as f2 :
  23. for line in f:
  24.  
  25. f2.write(line)
  26.  
  27. #----------------- 여러 파일 저장 (확인). 근데 내용이 아니라 파일명만 들어감.. ------------------
  28.  
  29.  
  30. import glob
  31.  
  32. list = glob.glob('A:/pycharm/study0722/data./*.txt')
  33. for fn in list :
  34. with open(fn, 'r') as f:
  35. with open(fn.replace('txt', 'csv'), 'w') as f2 :
  36. for line in f:
  37. tokens = line.split()
  38. year = int(tokens[0])
  39. month = int(tokens[1])
  40. day = int(tokens[2])
  41. tmax = float(tokens[3])
  42. tavg = float(tokens[4])
  43. tmin = float(tokens[5])
  44. rainfall = float(tokens[10])
  45. f2.write("{:d}, {:d}, {:d}, {:.1f}, {:.1f}, {:.1f}, {:.1f}".format(year, month, day, tmax, tavg, tmin, rainfall))
  46.  
  47. f2.write(line)
  48.  
  49. # -----------여러 파일을 수정한 후 저장 하기 1 실패-----------------------
  50.  
  51.  
  52.  
  53. import os
  54. output_dir = 'A:/pycharm/study0722/data'
  55. for path, dirs, files in os.walk ('A:/pycharm/study0722/data') :
  56. for f in files:
  57. with open(os.join('A:/pycharm/study0722/data/',f), 'r') as fr:
  58. with open(os.path.join(output_dir,f),'w') as fo:
  59. fo.write(f)
  60.  
  61. #---------------- 여러 파일 저장하기 2 실패 --------------
  62. #----------os.join이 없음.. (?)--------------
Add Comment
Please, Sign In to add comment