Skip to content

Conversation

@ghost
Copy link

@ghost ghost commented May 16, 2017

No description provided.


def print_all(f):
print f.read()

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.

Copy link
Author

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
Copy link

@ankit167 ankit167 May 17, 2017

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)

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)
Copy link
Contributor

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.

Copy link
Author

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.

Copy link
Contributor

@mam8 mam8 May 17, 2017

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"
    .......

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.

@geekcomputers geekcomputers merged commit 361f08e into geekcomputers:master May 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants