-3

So I've been taking a course on Python and the instructor that is teaching doesn't really explain somethings.

For example, the code I'm about to show has a line; f.write("This is line %d\r\n" % (i+1)). He just jumps right to the next part and doesn't explain the line of code (sometimes) (the %d, \r\n, etc.). This is how sometimes I "learn" a language and cannot even explain some lines.

I would like someone to explain to me what it does.

Code:

#
# Read and write files using the built-in Python file methods
#

def main():  
  # Open a file for writing and create it if it doesn't exist
  f = open("textfile.txt","w+")

  # write some lines of data to the file
  for i in range(10):
    f.write("This is line %d\r\n" % (i+1)) ## LINE OF CODE I want explained. (I know what write() is)
  
  # close the file when done
  f.close()
  
  # Open the file back up and read the contents
  f = open("textfile.txt","r")
  if f.mode == 'r': # check to make sure that the file was opened

    
    fl = f.readlines() # readlines reads the individual lines into a list
    for x in fl:
      print (x)
    
if __name__ == "__main__":
  main()

Also, if someone coulde link me to a page explaining all of this. Thanks.

hexerous
  • 221
  • 1
  • 12
  • I have searched for things but cannot find anything related to what I'm doing. – hexerous Sep 02 '20 at 19:28
  • Beside that - old style string formatting, not using a contexthandler for file handling (`with open(....) as f:)` - the `if f.mode == 'r'` when you just a line above opened it with `'r'` and not closing the file for the 2nd open shows that you might need a better source of teaching. – Patrick Artner Sep 02 '20 at 19:29
  • [string-formatting-vs-format-vs-string-literal](https://stackoverflow.com/questions/5082452/string-formatting-vs-format-vs-string-literal) – Patrick Artner Sep 02 '20 at 19:33
  • String formatting docs: https://docs.python.org/3.7/library/string.html File IO: https://docs.python.org/3/library/io.html – deponovo Sep 02 '20 at 19:36
  • Thank you for the references. – hexerous Sep 02 '20 at 19:38
  • The question would be rather better without the complaints about the teacher. – alani Sep 02 '20 at 19:42

2 Answers2

0

%d more or less specifies a placeholder for a number to be formatted into the string. \r\n are escape sequences, pretty much just telling the string to do a certain command. I.e. \r is return and \n inserts a new line.

So that write line is telling your program to insert (i+1) at %d and then insert a return and new line directly after.

Dharman
  • 30,962
  • 25
  • 85
  • 135
wvano97
  • 82
  • 6
  • What do you mean by return? Make a new **blank** line, or return that it wrote the line/skipped a line? Other than that thanks! – hexerous Sep 02 '20 at 19:35
  • 2
    @hexerous `return` is mostly an obsolete character these days and might not affect your output. Originally it was intended for printers, and would move the print head back to the left margin. Whereas `\n` line feed would advance to the next line without moving the print head. To get a new line you needed both. That convention is still used for text files in e.g. Windows. – Mark Ransom Sep 02 '20 at 19:43
0

f.write() basically writes in the text file that you opened on the first line. The argument in the write() function is the string that you want to write to the file. In this example, there is a loop that prints "This is line" and then what line number it is.

balexander1
  • 182
  • 1
  • 1
  • 8