brunch

매거진 파이썬

You can make anything
by writing

C.S.Lewis

by Master Seo Mar 08. 2020

56.  파이썬으로 파일 만들고 읽기

<1> 파일 만들기

<2> 툴설치



<1> 파일 만들기


1. 파이썬으로 파일 만들고 읽기


Centos 에서 파이썬 설치

[root@ip-10-0-0-128 ~]# yum install python



파일 만들기
[root@ip-10-0-0-108 ~]# more  1.py
test_file=open('1.txt','w')
test_file.write('start!!!!')
test_file.flush()
test_file.close()
[root@ip-10-0-0-108 ~]#

root@ip-10-0-0-108 ~]# python 1.py


[root@ip-10-0-0-108 ~]# more 1.txt

start!!!!

파일 읽기

read1.py

test_file=open('1.txt','r')

test-text=test_file.readline()

test_file.close()

print(read-text)


  



2줄 파일 만들기
[root@ip-10-0-0-108 ~]# more 11.py
test_file=open('11.txt','w')
test_file.write('start!!!!\n python' )
test_file.flush()
test_file.close()




[root@ip-10-0-0-108 ~]#python 11.py




[root@ip-10-0-0-108 ~]# more 11.txt
start!!!!
python




2줄 이상 파일 만들기

[root@ip-10-0-0-108 ~]# more  2.py
data=['1,2,3\n','4,5,6\n','7,8,9\n']
test_file=open('2.txt','w')
test_file.writelines(data)
test_file.close()

[root@ip-10-0-0-108 ~]# more 2.txt
1,2,3
4,5,6
7,8,9



2줄 읽기


[root@ip-10-0-0-108 ~]# more 22.py
test_file=open('11.txt','r')
lines = test_file.readlines()
print(lines)
test_file.close()
[root@ip-10-0-0-108 ~]#


읽고 공란 제거하기
[root@ip-10-0-0-108 ~]# python
Python 2.7.16 (default, Jul 19 2019, 23:05:17)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> test_file=open('11.txt','r')
>>> lines = test_file.readlines()
>>> print(lines)
['start!!!!\n', ' python']
>>> test_file.close()
>>> print(lines[0])
start!!!!

>>> print(lines[0].strip())
start!!!!
>>>



for문으로 2줄 이상 파일 읽기


[root@ip-10-0-0-108 ~]# more 33.py
test_file=open('2.txt','r')
for line in test_file:
print(line.strip())
test_file.close()


[root@ip-10-0-0-108 ~]# python 33.py
1,2,3
4,5,6
7,8,9


파일중 콤머(,) 없애고 읽기


[root@ip-10-0-0-108 ~]# more 44.py
test_file=open('2.txt','r')
for line in test_file:
a1=line.strip().split(',')
a2='\t'.join(a1)
print(a2)
test_file.close()


[root@ip-10-0-0-108 ~]# python 44.py
1 2 3
4 5 6
7 8 9



콤마(,)  제거하기 -  split(',')

- 추가하기


[root@ip-10-0-0-108 ~]# more 44.py
test_file=open('2.txt','r')
for line in test_file:
a1=line.strip().split(',')
a2='-'.join(a1)
print(a2)
test_file.close()


[root@ip-10-0-0-108 ~]# python 44.py
1-2-3
4-5-6
7-8-9
[root@ip-10-0-0-108 ~]#


참고 사이트 https://www.w3schools.com/python/python_file_write.asp




<2> 툴설치


1.  PC에 Python 실행 툴 설치

Python 개발환경 툴 아나콘다 https://www.anaconda.com/distribution/

https://brunch.co.kr/@topasvga/869

pycharm 설치 https://www.jetbrains.com/pycharm/

VS Code   https://code.visualstudio.com/download


2. PC에  편집 툴 설치

subline text https://www.sublimetext.com/3

프로그래머블 노트 패드 http://www.pnotepad.org/

노트패드++  https://notepad-plus-plus.org/downloads/



3. 공부시 참고 사이트

https://www.w3schools.com/python/python_variables.asp


감사합니다.


매거진의 이전글 55. 파이썬과 장고를 이용해 웹 사이트를 만들어보자

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari