[Python] 입출력
Y
Yerim
a = input()
b = input("안내_문구")# 정수
a = 123
print(a) # 123
# 문자열
# 따옴표로 둘러싸인 문자열을 연속해서 쓰면 "+" 연산과 같음
print("life" "is" "too short") # lifeistoo short
print("life"+"is"+"too short") # lifeistoo short
# 문자열 띄어쓰기는 쉼표
print("life", "is", "too short") # life is too short
# 한 줄에 결괏값 출력 - end로 끝 문자 지정
for i in range(10):
print(i, end=' ') # 0 1 2 3 4 5 6 7 8 9파일_객체 = open(파일_이름, 파일_열기_모드)파일열기모드 | 설명 |
r | 읽기 모드: 파일을 읽기만 할 때 사용한다. |
w | 쓰기 모드: 파일에 내용을 쓸 때 사용한다. |
a | 추가 모드: 파일의 마지막에 새로운 내용을 추가할 때 사용한다. |
파일_객체.close()# file_with.py
with open("foo.txt", "w") as f:
f.write("Life is too short, you need python")# write_data.py
f = open("C:/doit/새파일.txt", 'w')
for i in range(1, 11):
data = "%d번째 줄입니다.\n" % i
f.write(data)
f.close()# readline_all.py
f = open("C:/doit/새파일.txt", 'r')
while True:
line = f.readline()
if not line: break
print(line)
f.close()# readlines.py
f = open("C:/doit/새파일.txt", 'r')
lines = f.readlines()
for line in lines:
line = line.strip()
print(line)
f.close()# read.py
f = open("C:/doit/새파일.txt", 'r')
data = f.read()
print(data)
f.close()# read_for.py
f = open("C:/doit/새파일.txt", 'r')
for line in f:
print(line)
f.close()# add_data.py
f = open("C:/doit/새파일.txt",'a')
for i in range(11, 20):
data = "%d번째 줄입니다.\n" % i
f.write(data)
f.close()# sys1.py
import sys
args = sys.argv[1:]
for i in args:
print(i)C:\doit>python sys1.py aaa bbb ccc