-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Add files via upload #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add files via upload #166
Conversation
|
|
||
| def print_all(f): | ||
| print f.read() | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use two spaces between functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I type this code on debian's gedit, in gedit nothing automatic indentation, so the result like this...
| rewind(current_file) | ||
|
|
||
| print "Let's print three lines:" | ||
| current_line = 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to use a loop to call print_a_line(). Would reduce Lines of Code.
| def print_a_line(line_count, f): | ||
| print line_count, f.readline() | ||
|
|
||
| current_file = open(input_file) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please wrap non-function code inside main()
| def print_a_line(line_count, f): | ||
| print line_count, f.readline() | ||
|
|
||
| current_file = open(input_file) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the input_file is not existing, the open will fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code run on command line, so must there is a file in same directories.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am also a newbie. I just learned a good way for this. It's with ... as ...:
with open( input_file) as current_file:
print "First let's print the whole file:\n"
.......
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sahrulhs The code is generic. Ideally, it should handle every case, which includes the file not being present at all.
No description provided.