All the answers here have already stated why you get an error message. So, I will predominantly focus on the line by line explanation of the program.
#!/usr/bin/env python
This line specifies that the file is a python script and also specifies which python environment is being used.
Read this for better understanding:
Why do people write #!/usr/bin/env python on the first line of a Python script?
The next line is
# 'makeTextFile.py -- create text file'
This is a comment line which specifies what this current script does.
import os
ls = os.linesep
This line is importing a module named os.
A module is a file consisting of Python code. A module can define functions, classes and variables.In this case, the module imported provides a way of using operating system dependent functionality.
It then stores the line separator(a string that separates one line from another) used in files (specific to that operating system).
Read about os modules here:-
https://docs.python.org/2/library/os.html
After this step, you have to get the fname (inside the loop) as stated in one of the answers. 'fname' is a variable where the filename to be created is stored.
while True:
if os.path.exists(fname):
print "ERROR: '%s' already exists" % fname
else:
break
This is a while loop (a repeating code block that continues till the condition (here True) is true). It checks if a file with that specific name exists.This is done with the help of the os.path manipulator . If it exists , then there is an Error which is printed inside the (if:) block and this loop repeats till a filename that does not exist is encountered. In that case (filename does not exist ) , break is encountered which breaks out of the while loop.
all = []
This specifies a list data structure named all . The motivation of this declaration is that it will store all the user input .
print "\nEnter lines (' . ' by itself to quit). \n"
This prints a line into the interpreter screen , that suggests the user to input their message.It is a hint to the user as to what is expected of them. It also specifies that to quit enter '.' only.
while True:
entry = raw_input('> ')
if entry == '.':
break
else:
all.append(entry)
This is another loop which iteratively takes user input and appends ( adds into the list) to all . If a single '.' is encountered, the loop breaks and the user input is over.
Having finished this, the next step is to write it into the file. The next set of code is written for that purpose.
fobj = open(fname, 'w')
fobj.writelines(['%s%s' % (x, ls) for x in all])
fobj.close()
The file is opened in 'write' mode . Mode specifies the purpose for which the file is opened. Here ,'w' specifies that the file opened is only for writing purpose. The file opened is accessed by the variable fobj. The next line then writes the contents of all(the list) into the file. 'for x in all' iterates through each item inside the list. This x and the line separator (stored in ls) is then written . Finally, after all the contents are written, the file is closed.
The author then prints
print 'DONE'
It just shows that the entire work is over. This is a redundant step and can be avoided.
Correction in the approach will be appreciated.