본문으로 바로가기
반응형

파이썬에서 파일을 읽고 쓰는 방법과, 네트워크에 접속하여 특정 url을 불러오는 코드는 다른 언어에 비해 매우 쉽습니다.

간단하게 예제로 따라할 수 있으니 후르륵 보면 되겠네요.


File Read

### file read
f1 = open("./sampletext", mode="r")
print(f1.read())
f1.close()


간단합니다.

file을 open하고, read()해서 읽은뒤에 close로 닫아 줍니다.

결과

Intellecture 지적인

You should take care of your intellectual property.


elaborate 상세한, 정교한


He explained the process in elaborate detail.

The books show elaborate works of creation that have been preserved for centuries.


I/O 작업은 대부분 close 작업을 수반합니다.

(사용할 리소스를 반환하는 작업이 항상 필요합니다.)

따라서 with 구문을 이용하여 사용이 끝나면 알아서 close 하도록 사용할 수도 있습니다.

with open("./sampletext", mode="r") as f2:
    print("read(): " + f2.read())
    print("--------")
    
with open("./sampletext", mode="r") as f3:
    print("read(n):" + f3.read(10))
    print("--------")
    
with open("./sampletext", mode="r") as f4:
    print("readline():" + f4.readline())
    print("--------")
    
with open("./sampletext", mode="r") as f5:
    print("readlines():" + str(f5.readlines()))


with를 사용하고 다양한 read 함수를 사용했습니다.

  • read(n): 해당 개수만 큼 읽는다.
  • readline(): 개행문자를 기준으로 line 단위로 읽는다.
    • 따라서 전체를 읽으려면 for문으로 반복해야 한다.
  • readlines(): 개행문자를 기준으로 line을 읽어 list로 반환한다.

결과
read(): Intellecture 지적인
You should take care of your intellectual property.

elaborate 상세한, 정교한

He explained the process in elaborate detail.
The books show elaborate works of creation that have been preserved for centuries.
--------
read(n):Intellectu
--------
readline():Intellecture 지적인

--------
readlines():['Intellecture 지적인\n', 'You should take care of your intellectual property.\n', '\n', 'elaborate 상세한, 정교한\n', '\n', 'He explained the process in elaborate detail.\n', 'The books show elaborate works of creation that have been preserved for centuries.']


File write

with open("./sampletext", mode="r") as f6:
    strlines = f6.readlines()
    with open("./smapleWrite", mode="w") as fwrite:
        for string in strlines:
            fwrite.write(string)


코드 자체가 어렵지 않습니다.

예제에서는 개행문자 단위로 읽어 파일에 씁니다.

만약 binaray 파일이라면 다른 형태로 읽어서 써야 겠죠?


pickle

Object를 파일로 쓰고 읽기 위해서 파이썬에는 pickle이라는 library를 제공합니다.
따라서 serialize와 unserialize를 하기 위해서는 pickle을 import 해야 합니다.

### pickle
import pickle

listsample = [1,2,3]

with open("./listSample.pickle", "wb") as outputf:
    pickle.dump(listsample, outputf)

    
with open("./listSample.pickle", "rb") as inputf:
    while True:
        try:
            print(str(pickle.load(inputf)))
        except EOFError:
            break


위 예제에서는 list를 pickle을 이용하여 serialize하여 파일에 쓰고, 이를 다시 읽어 list 객체를 만듭니다.

이때 사용되는 함수는 pickle.dump / pickle.load 입니다.

또한 pickle.load는 l파일을 다읽고 나면 EOF error를 반환하게 때문에 try-except 문으로 처리해 줍니다.


url connection

다른 언어에서는 url connection을 맺기 위해 많은 코드가 필요하지만 (물론 잘 정리된 library들도 있지만) 파이썬에서는 간단하게 urllib.request를 이용하여 처리할 수 있습니다.
어마무시하게 간단하죠...
### url connection
import urllib.request

try:
    url = "https://sqlite.org/fts5.html#full_text_query_syntax"
  
    with urllib.request.urlopen(url) as doc:
        html = doc.read()
        print(html)
except:    
    print("Open error!!")


urlopen 함수를 이용하여 해당 페이지를 불러오며, 결과는.. 해당 페이지의 html 문서를 모두 불러 옵니다.


url parse / unparse

url은 주소 뿐만 아니라 같이 넘어가는 parameter등도 같이 표현됩니다.
이를 정규 표현식이나, string match로 분리하기 보다는 각 원소별로 간단히 분리할 수 있도록 url parse 기능을 제공합니다.
### url connection
import urllib.request

url = "https://sqlite.org/fts5.html#full_text_query_syntax"
parsed = urllib.parse.urlparse(url)
print("parsed url: " + str(parsed))
print("---------------------")
unparsed = urllib.parse.urlunparse(parsed)
print("unparsed url:" + str(unparsed))


urlparseurlunparse는 서로 상반되는 작업으로 url을 각 원소단위로 분리하고, 다시 재조합 하는 역할을 합니다.

unparse로 재조합된 url은 원본과 표현은 다를 수 있으나, 실제 동작은 동일합니다.


결과

parsed url: ParseResult(scheme='https', netloc='sqlite.org', path='/fts5.html', params='', query='', fragment='full_text_query_syntax')

---------------------

unparsed url:https://sqlite.org/fts5.html#full_text_query_syntax



반응형