FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
article/corepython2ndsourcecode/ch03/makeTextFile.py at master · pythonpeixun/article · GitHub
pythonpeixun
/
article
Public
Notifications
You must be signed in to change notification settings
Fork
259
Star
677
Code
Pull requests
0
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
article
/
corepython2ndsourcecode
/
ch03
/
makeTextFile.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (38 loc) · 1.03 KB
Breadcrumbs
article
/
corepython2ndsourcecode
/
ch03
/
makeTextFile.py
Copy path
File metadata and controls
44 lines (38 loc) · 1.03 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
# coding:utf-8
'''
欢迎参加黄哥python远程视频培训,
帮你完成从不会写代码到会写代码解决问题的过渡。
https://github.com/pythonpeixun/article/blob/master/index.md
咨询qq:1465376564
黄哥Python培训 黄哥改写
Python 3
'makeTextFile.py -- create text file'
P52页 中文版书上错误点
1、少一个变量fname = input('Enter file name: ')
2、书上11-14行(下面的代码22-26) 行代码没有缩进
'''
import
os
ls
=
os
.
linesep
# get filename
while
True
:
fname
=
input
(
'Enter file name: '
)
if
os
.
path
.
exists
(
fname
):
print
(
"ERROR: '%s' already exists"
%
fname
)
else
:
break
# get file content (text) lines
all
=
[]
print
(
"
\n
Enter lines ('.' by itself to quit).
\n
"
)
# loop until user terminates input
while
True
:
entry
=
input
(
'> '
)
if
entry
==
'.'
:
break
else
:
all
.
append
(
entry
)
# write lines to file with proper line-ending
fobj
=
open
(
fname
,
'w'
)
fobj
.
writelines
([
'%s%s'
%
(
x
,
ls
)
for
x
in
all
])
fobj
.
close
()
print
(
'DONE!'
)
Back
|
FazBrowse Home
|
New Git URL