파일 만들기 [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
[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 ~]# 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()