���� Python �̳� | ��12�� ����/��� | |
---|---|---|
��һҳ | �ļ� | ��һҳ |
Ŀ¼��
�ںܶ�ʱ�������Ҫ����ij������û������������Լ��������������û�����õ����룬Ȼ���ӡһЩ��������ǿ��Էֱ�ʹ��raw_input
��print
����������Щ���ܡ������������Ҳ����ʹ�ö��ֶ�����str
���ַ������ࡣ���磬���ܹ�ʹ��rjust
�������õ�һ����һ�������Ҷ�����ַ���������help(str)
��ø������顣
��һ�����õ�����/��������Ǵ����ļ�������������д�ļ����������������������ģ����ǽ���������̽�����ʵ����Щ���ܡ�
�����ͨ������һ��file
��Ķ�������һ���ļ����ֱ�ʹ��file
���read
��readline
��write
������ǡ���ض�д�ļ������ļ��Ķ�д�������������ڴ��ļ�ʱָ����ģʽ���������ɶ��ļ��IJ�����ʱ�������close
����������Python��������˶��ļ���ʹ�á�
#!/usr/bin/python
# Filename: using_file.py
poem =
'''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
f =
file
(
'poem.txt'
,
'w'
)
# open for 'w'riting
f.write(poem)
# write text to file
f.close()
# close the file
f =
file
(
'poem.txt'
)
# if no mode is specified, 'r'ead mode is assumed by default
while
True
:
line = f.readline()
if
len
(line) ==
0
:
# Zero length indicates EOF
break
print
line,
# Notice comma to avoid automatic newline added by Python
f.close()
# close the file
��Դ�ļ���code/using_file.py��
$ python using_file.py
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
���ȣ�����ͨ��ָ������ϣ�����ļ���ģʽ������һ��file
���ʵ����ģʽ����Ϊ��ģʽ��'r'
����дģʽ��'w'
������ģʽ��'a'
������ʵ�ϻ��ж�ö��ģʽ����ʹ�ã������ʹ��help(file)
���˽����ǵ����顣
����������дģʽ���ļ���Ȼ��ʹ��file
���write
������д�ļ������������close
�ر�����ļ���
��������������һ�δ�ͬһ���ļ������ļ����������û��ָ��ģʽ����ģʽ����ΪĬ�ϵ�ģʽ����һ��ѭ���У�����ʹ��readline
�������ļ���ÿһ�С�����������ذ�����ĩ���з���һ�������С����ԣ���һ�� �յ� �ַ��������ص�ʱ����ʾ�ļ�ĩ�Ѿ������ˣ���������ֹͣѭ����
ע�⣬��Ϊ���ļ������������Ѿ��Ի��з���β������������print
�����ʹ�ö����������Զ����С����������close
�ر�����ļ���
���ڣ�����һ��poem.txt
�ļ�����������֤����ȷʵ���������ˡ�
��һҳ | ��һ�� | ��һҳ |
---|---|---|
���� | ��ҳ | ������ |