#!/usr/bin/env python # coding: utf-8 # ## 06-1 내가 프로그램을 만들 수 있을까? # # > 프로그램을 만들려면 가장 먼저 "입력"과 "출력"을 생각하라. # # ### 구구단 프로그램 작성하기 # # - 함수 이름은 GuGu # - 프로그램 실행 시 몇단을 출력할지 입력을 받는다. # - 출력형태: 2 * 1 = 2 # In[3]: def GuGu(): n = int(input("몇 단을 출력하시겠습니까? ")) for i in range(1, 10): print("%d * %d = %d" % (n, i, n*i)) # In[4]: GuGu() # In[5]: GuGu() # --- # ## 06-2 3과 5의 배수 합하기 # # >- 10 미만의 자연수에서 3과 5의 배수를 구하면 3, 5, 6, 9이다. 이들의 총합은 23이다. # >- 1000 미만의 자연수에서 3의 배수와 5의 배수의 총합을 구하라. # In[2]: sum = 0 for n in range(1, 1000): if n%3 == 0 or n%5 == 0: sum += n print(sum) # --- # ## 06-3 게시판 페이징하기 # # - 함수 이름은? getTotalPage # - 입력 받는 값은? 게시물의 총 건수(m), 한 페이지에 보여줄 게시물 수(n) # - 출력하는 값은? 총 페이지수 # In[3]: def getTotalPage(m, n): if m % n == 0: return m // n else: return m // n + 1 print(getTotalPage(5, 10)) print(getTotalPage(15, 10)) print(getTotalPage(25, 10)) print(getTotalPage(30, 10)) # --- # ## 06-4 간단한 메모장 만들기 # # 원하는 메모를 파일에 저장하고 추가 및 조회가 가능한 간단한 메모장을 만들어 보자. # # - 필요한 기능은? 메모 추가하기, 메모 조회하기 # - 입력 받는 값은? 메모 내용, 프로그램 실행 옵션 # - 출력하는 값은? memo.txt # # # - 다음과 같은 명령을 실행했을 때 메모를 추가할 수 있도록 만들어 보자. # # > python memo.py -a "Life is too short" # In[4]: get_ipython().run_cell_magic('writefile', 'memo.py', 'import sys\n\noption = sys.argv[1]\nmemo = sys.argv[2]\n\nif option == "-a":\n f = open("memo.txt", "a")\n f.write(memo)\n f.write("\\n")\n f.close()\nelif option == "-v":\n f = open("memo.txt", "r")\n memo = f.read()\n f.close()\n print(memo)\n') # In[5]: get_ipython().run_line_magic('run', 'memo.py -a "Life is too short"') # In[6]: get_ipython().system('more memo.py') # In[7]: get_ipython().system('more memo.txt') # In[8]: get_ipython().run_line_magic('run', 'memo.py -a "You need python"') # In[9]: get_ipython().system('more memo.txt') # In[10]: get_ipython().run_line_magic('run', 'memo.py -v') # In[11]: get_ipython().run_cell_magic('writefile', 'memo.py', 'import sys\n\noption = sys.argv[1]\n\nif option == "-a":\n memo = sys.argv[2]\n f = open("memo.txt", "a")\n f.write(memo)\n f.write("\\n")\n f.close()\nelif option == "-v":\n f = open("memo.txt", "r")\n memo = f.read()\n f.close()\n print(memo)\n') # In[12]: get_ipython().run_line_magic('run', 'memo.py -v') # --- # ## 06-5 탭을 4개의 공백으로 바꾸기 # # 문서 파일을 읽어서 그 문서 파일 내에 있는 탭(tab)을 공백(space) 4개로 바꾸어주는 스크립트를 작성해 보자. # # - 필요한 기능은? 문서 파일 읽어 들이기, 문자열 변경하기 # - 입력 받는 값은? 탭을 포함한 문서 파일 # - 출력하는 값은? 탭이 공백으로 수정된 문서 파일 # # # - 다음과 같은 형식으로 프로그램이 수행되도록 만들 것이다. # # > python tabto4.py src dst # In[1]: get_ipython().run_cell_magic('writefile', 'a.txt', 'Life\tis \ttoo\tshort\nYou\tneed\tPython\n') # In[2]: get_ipython().system('more a.txt') # In[4]: get_ipython().run_cell_magic('writefile', 'tabto2.py', 'import sys\n\nsrc = sys.argv[1]\ndst = sys.argv[2]\n\nf = open(src, "r")\ntab = f.read()\nf.close()\n\nf = open(dst, "w")\nf.write(tab.replace("\\t", " "*2))\nf.close()\n') # In[5]: get_ipython().run_line_magic('run', 'tabto2.py a.txt b.txt') # In[6]: get_ipython().system('more b.txt') # --- # ## 06-6 하위 디렉터리 검색하기 # # - 특정 디렉터리부터 시작해서 그 하위의 모든 파일 중 파이썬 파일만 출력 # In[1]: import os print(os.listdir("./")) # In[2]: filenames = os.listdir("./") for filename in filenames: fullname = os.path.join("./", filename) print(fullname) # In[5]: def search(dirname): filenames = os.listdir(dirname) for filename in filenames: fullname = os.path.join(dirname, filename) ext = os.path.splitext(fullname)[-1] if ext == '.py': print(fullname) # In[6]: search('./') # In[7]: def search(dirname): try: filenames = os.listdir(dirname) for filename in filenames: fullname = os.path.join(dirname, filename) if os.path.isdir(fullname): search(fullname) else: ext = os.path.splitext(fullname)[-1] if ext == '.py': print(fullname) except PermissionError: pass # In[8]: search("./") # ### [하위 디렉터리 검색을 쉽게 해주는 os.walk] # # - 시작 디렉터리부터 시작하여 그 하위의 모든 디렉터리를 차례대로 방문하게 해주는 함수 # In[11]: import os for (path, dir, files) in os.walk("./"): for filename in files: ext = os.path.splitext(filename)[-1] if ext == '.py': print("%s/%s" % (path, filename))