본문 바로가기

전체 글

Python 어렵게 배우기 - Exercise 20: Functions and Files 원문 : http://learnpythonthehardway.org/book/ex20.html 테스트에 사용할 파일 내용(test.txt)this is the first linecontentsfinish file 명을 입력받고 file을 열어서 그 내용을 화면에 인쇄하는 프로그램을 만들어 보자 (ex20.py) from sys import argv script, input_file = argv def print_all(f): print f.read() def rewind(f): f.seek(0) def print_a_line(line_count, f): print line_count, f.readline() current_file = open(input_file) print "First let's pri.. 더보기
Python 어렵게 배우기 - Exercise 19: Functions and Variables 원문 : http://learnpythonthehardway.org/book/ex19.html 숫자형 매개변수 2개를 입력받아 인쇄하는 함수를 작성하고 테스트 해보자 (ex19.py) def cheese_and_crackers(cheese_count, boxes_of_crackers): print "You have %d cheeses!" % cheese_count print "You have %d boxes of crackers!" % boxes_of_crackers print "Man that's enough for a party!" print "Get a blanket.\n" print "We can just give the function numbers directly:" cheese_and_cra.. 더보기
Python 어렵게 배우기 - Exercise 18: Names, Variables, Code, Functions 원문 : http://learnpythonthehardway.org/book/ex18.html 함수를 만들고 매개변수(parameter)를 넘겨 보자 (ex18.py) # this one is like your scripts with argv def print_two(*args): arg1, arg2 = args print "arg1: %r, arg2: %r" % (arg1, arg2) # ok, that *args is actually pointless, we can just do this def print_two_again(arg1, arg2): print "arg1: %r, arg2: %r" % (arg1, arg2) # this just takes one argument def print_one(.. 더보기
oAuth 를 쉽고 편하게!!! oAuth 솔루션이 있었네 https://oauth.io/ google, facebook, twitter, instagram, youtube, ... 엄청나게 많은 api를 제공한다. 더보기
Python 어렵게 배우기 - Exercise 17: More Files 원문 : http://learnpythonthehardway.org/book/ex17.html 파일과 관련된 다른 기능들 (ex17.py) from sys import argv from os.path import exists script, from_file, to_file = argv print "Copying from %s to %s" % (from_file, to_file) # we could do these two on one line too, how? in_file = open(from_file) indata = in_file.read() print "The input file is %d bytes long" % len(indata) print "Does the output file exist? .. 더보기
Python 어렵게 배우기 - Exercise 16: Reading and Writing Files 원문 : http://learnpythonthehardway.org/book/ex16.html 파일을 읽고 써 보자 (ex16.py) from sys import argv script, filename = argv print "We're going to erase %r." % filename print "If you don't want that, hit CTRL-C (^C)." print "If you do want that, hit RETURN." raw_input("?") print "Opening the file..." target = open(filename, 'w') print "Truncating the file. Goodbye!" target.truncate() print "Now I'm g.. 더보기
Python 어렵게 배우기 - Exercise 15: Reading Files 원문 : http://learnpythonthehardway.org/book/ex15.html 읽을 파일을 먼저 만들자(ex15_sample.txt)This is stuff I typed into a file.It is really cool stuff.Lots and lots of fun to have in here. 파일을 읽어보자 (ex15.py) from sys import argv script, filename = argv txt = open(filename) print "Here's your file %r:" % filename print txt.read() print "Type the filename again:" file_again = raw_input("> ") txt_again = ope.. 더보기
Python 어렵게 배우기 - Exercise 14: Prompting and Passing 원문 : http://learnpythonthehardway.org/book/ex14.html 앞에서 공부한 raw_input, argv를 다시 사용 해 보자 (ex14.py) from sys import argv script, user_name = argv prompt = '> ' print "Hi %s, I'm the %s script." % (user_name, script) print "I'd like to ask you a few questions." print "Do you like me %s?" % user_name likes = raw_input(prompt) print "Where do you live %s?" % user_name lives = raw_input(prompt) print.. 더보기