User:Milanand/Python 3 Programming/Input and Output

Input
Python includes the function, which accepts data directly from the user. There are also very simple ways of reading a file and, for stricter control over input, reading from stdin if necessary.

input
asks the user for a string of data (ended with a newline), and simply returns the string. It can also take an argument, which is displayed as a prompt before the user enters the data. E.g. prints out What is your name? Example: in order to assign the user's name, i.e. string data, to a variable "x" you would type Once the user inputs their name, e.g. Delilah, you can call it as x. prints out Your name is Delilah

To evaluate a given string returned by  as if it were a Python programme, you can use. So entering would return a list containing those numbers, just as if it were assigned directly in the Python script. More complicated expressions are possible. For example, if a script says: it is possible for a user to input: which yields the correct answer in list form. Note that no inputted statement can span more than one line. should not be used for anything but the most trivial programme. Turning the strings returned from   into python types using an idiom such as: is preferable, as the literal is turned into a python type. This will allow a malicious person to run arbitrary code from inside your program trivially.

File Objects
In Python, files can be opened by using the built-in  function: This means f is open for reading. The first argument is the filename and the second parameter is the mode, which can be 'r', 'w', or 'x', among some others. The most common way to read from a file is simply to iterate over the lines of the file: This will print the first character of each line. Note that a newline is attached to the end of each line read this way. The newer and better way to read from a file: Here, the opened file will close itself after having read each line. Because files are automatically closed when the file object goes out of scope, there is no real need to close them explicitly. So, the loop in the previous code can also be written as: A file object contains a marker to represent the current position. If the file marker should be moved back to the beginning, one can either close the file object and reopen it or just move the marker back to the beginning with:

Standard File Objects
Like many other languages, there are built-in file objects representing standard input, output, and error. These are in the  module and are called ,  , and. There are also immutable copies of these in,  , and. This is for IDLE and other tools in which the standard files have been changed. You must import the sys module to use the special stdin, stdout, stderr I/O handles. For finer control over input, use. In order to implement the UNIX 'cat' program in Python, you could do something like this: Note that  will read from standard input until EOF. (which is usually Ctrl+D.)

Parsing command line
Command-line arguments passed to a Python program are stored in  list. The first item in the list is the name of the Python program, which may or may not contain the full path depending on the manner of invocation. list is modifiable. Printing all passed arguments except for the program name itself: Parsing passed arguments for flags: In the above code, the arguments at which options are found are removed so that  can be looped for all remaining arguments. Parsing of command-line arguments is further supported by library modules  and   (which can be used in a similar fashion to the C   function) Links for further reference:


 * Python 3 Documentation - sys
 * Python 3 Documentation - argparse
 * Python 3 Documentation - getopt

Output
The basic way to do output is the  function. To print multiple things on the same line separated by spaces, use commas between them, like this: This will print out the following: Hello, World! While neither string contained a space, a space was added by  because of the comma between the two objects. Arbitrary data types can be printed this way: This will output the following: 1 2 255 511 (10+5j) -0.999   Objects can be printed on the same line without needing to be on the same line if one uses the  keyword: This will output the following: 0 1 2 3 4 5 6 7 8 9 To end the printed line with a newline, add an empty  call. This will output the following: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 If the empty function call to  were not present, the above output would look like: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 You can use similar syntax when writing to a file instead of to standard output, like this: This will print to any object that implements, which includes file objects.

File Output
Printing numbers from 1 to 10 to a file, one per line: With 'w', the file is opened for writing. The keyword  specifies that   should send its output to a file rather than standard output. Printing numbers from 1 to 10 to a file, separated with a dash: Opening a file for appending rather than overwriting:

Formatting
Formatting numbers and other values as strings using the format string method: Formatting numbers and other values as strings using literal string interpolation: Links for further reference:


 * Python 3 Documentation - -style String Formatting
 * Python 3 Documentation - Built-in  function
 * Python 3 Documentation - Custom String Formatting
 * Python 3 Documentation - Format Specification Mini Language
 * Python 3 Documentation - Template Strings
 * PEP 3101 - Advanced String Formatting
 * PEP 498 - Literal String Interpolation