Table of Contents



The print() function send a string object to the Standard Output device which is usually the screen or console.

Any object which can be converted to a string can be printed to the screen, so a list, dictionary, tuple, or math expression, etc. may be printed.

Print function takes a number of useful arguments:

Parameter Description
object(s) Essential : Any number of objects, will be converted to strings
sep='separator' Optional : How objects will be separated. Default is space' '
end='end' Optional. : What to print at the end. Default is '\n' (line feed)
file Optional. : Where the objects will be writen. Default is sys.stdout
flush Optional. : Will the output be flushed (True) or buffered (False). Default is False

So any number of objects may be printed to console or to file, separated by spaces or commas or linefeeds, and ending in linefeed or none at all. Print() with arguments, will give a newline.

  x = 12
  y = 22
  print("Hello World")      =brackets
  print("The answer is", 2*2)   =operation
  print(x, end=" ")     =end may be \n, space or none to join multiple prints
  print(x, y,  end="\n")
  print("fatal error", file=sys.stderr)
  print((x, y)) =tuple
  print("There are <", 2**32, "> possibilities!", sep=" ")


As data can be printed to the standard output (STDOUT), it can also be taken from user through the standard input device which is usually the keyboard. For this the data input is assigned to a variable using the input() function which has a prompt message to the user as a parameter.

  name = input("Enter your name: ")
  print("Hello "+ name)

The input is always a string type, so if you divide input by a number you will get an error. To overcome this, change the type of variable by converting string to integer or float.

  number = input("Enter number :  ")
  print(number/2) ==> TypeError: unsupported operand type(s) for /: 'str' and 'int'


To create an empty file open it for writing and then close it.

  open(file, 'w').close()

The Open() function is a built-in function (in module io) that opens a file for reading, writing or appending input.

  f = open("c:/new.txt",  "w") 
  f.write("Hello World")

Syntax : open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

The input can be text or binary data, in any encoding style.

It takes the filename or the path as the first parameter, then which mode you want to use with the file, writing, reading or appending. This is usually a quoted character (string): "r" for reading, "w" for writing, and "a" for appending. It defaults to 'rt' which means open for reading in text mode. So, if you want to open a file for reading text only, you may use just open(filename).

It returns a stream object which needs to be closed at the end.

Other mode values are 'w' for writing which overwrite. The 'x' mode implies 'w' and raises an FileExistsError if the file already exists., and 'a' for appending to the end of the file regardless of the current seek position.

In text mode, if encoding is not specified the encoding used is platform dependent. For reading and writing raw bytes use binary mode and leave encoding unspecified.) The available modes are:

Character Meaning
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
  file = open("c:/new.txt", "a")
  seq ="""
    \nThis is line 1
    \nThis is line 2
    \nThis is line 3


Python distinguishes between files opened in binary and text modes. Files opened in binary mode (appending 'b' to the mode argument) return contents as bytes objects without any decoding. This is useful in some programming operations. If you want to convert it to In text mode, encode it first using the specified encoding if given. Encoding is also used when the contents of a file contains bytes which cannot be displayed on the screen, for example if the file was saved in Unicode.

Another way to overcome, non_ASCII characters is to ignore errors using the flag parameter error="ignore".

Buffering is an optional integer used to set the buffering policy. - 0 to switch buffering off (only allowed in binary mode) - 1 to select line buffering (only usable in text mode) - integer > 1 to indicate the size of a fixed-size chunk buffer.

Encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent, but any encoding supported by Python can be passed.

Errors is an optional string that specifies how encoding errors are to be handled

  • 'strict' to raise a ValueError exception if there is an encoding error
  • the default of None has the same effect
  • 'ignore' to ignore errors.(Note that ignoring encoding errors can lead to data loss.

Newline controls how universal newlines works- it only applies to text mode. It can be None,"",'\n','\r', and '\r\n'".

It works as follows:

  • On input, if newline is None, universal newlines mode is enabled.
  • Lines in the input can end in '\n', '\r', or '\r\n' and these are translated into '\n'
  • If it is "", universal newline mode is enabled, but line endings are returned to the caller untranslated.
  • On output, if newline is None, any '\n' characters written are translated to the system default line separator.
  • If newline is '' or '\n', no translation takes place.

open() returns a file object whose type depends on the mode, and through which the standard file operations such as reading and writing are performed.

  open("c:/new.txt", "r").read()
  f = open("c:/",  "r")
  string =

If you use 'with open as' the file is closed after operation is completed.

    with open("c:/test.txt") as f:
        for line in f:
            print line
    for line in open("c:/test.txt"):
        print line  =file still open

Open file by its default executable program

  import os


To rename a file, you need the os module whioch has a rename function.

  import os
  os.rename("c:/new.txt", "c:/new.bak")


To delete a named file use os module remove function.

  import os
  file = "file_name.txt"

check if file is empty (zero size)


check if file actually exists

file = "c:/scripts/tut/file.txt"


If you have a name of a file, you may need to check its status : is it a file or directory? Does it exists or you need to create it? Is it empty or does it contains some data?

is it empty (zero size)


Does the file exists

  file = "c:/scripts/tut/file.txt"

is it a name for file or directory

The os module has specific functions: os.path.isfile() and os.path.isdir()

  file = "c:\pw.txt"
  dir = "c:\py"


Create Directory

You create (make) a new directory using the os module. To avoid errors, check first that the directory does not exist :

  import os
  if not os.path.exists("c:/test"):

Remove Directory

To delete a named directory, use the remove directory - rmdir - function in the os module:

  import os

Change Current Directory

To change working directory to another directory, use the change directory function - chdir - in the os module:

  import os

If you want to change directory to the current directory where the script is located, use the get current directory function as an argument in the chdir function.


To Get the name of the Current Directory

  import os

To List Directory Content

To list all entries in a directory including both files and folders, us listdir() function in os module.


To get a separate list of files, path and directories use the walk() function in os module. It walks the tree of directory branches and returns a tuple of three lists:

  • dirpath : a string, the path to the directory being walked.
  • dirnames : a list of the names of the subdirectories in dirpath (excluding '.' and '..').
  • filenames : a list of the names of the non-directory files in dirpath.

The names in the two lists are just names, with no path components. To get a full path (which begins with top) to a file or directory use:

  os.path.join(dirpath, name).

Syntax : os.walk = walk(top, topdown=True, onerror=None, followlinks=False)

    import os
    [print(root) for (root, subdirs, files) in os.walk('d:/py')]
    [print(subdirs) for (root, subdirs, files) in os.walk('d:/py')]
    [print(files) for (root, subdirs, files) in os.walk('d:/py')]

Another way to get names of all files with their fullpath is to use the glob module:

  import glob
  glob.glob("d:/**/*") =get all files in a list

To list folders only in the directory, use os.path isdir with glob:

  from glob import glob
  for d in glob("c:/*/"):
    if os.path.isdir(d):
     print d

Similarly, to list files only, use os.path.isfile() function:

  from glob import glob
  for f in glob("c:/*"):
    if os.path.isfile(f):
     print f

It is possible to list files in subdirectories for one or two levels, using glob module:

  import glob
  list = glob.glob("c:/py/*/**")
  for i in list:
   if os.path.isdir(i):
     print "Directory: ", i
     print "File     : ", i

To select specific files, you can specify their extension or some characters in their names.

  import glob
  list = glob.glob("c:/py/*.txt")
  list = glob.glob("c:/py/c*t.txt")
  list = glob.glob("c:/py/*z?.txt")

  import fnmatch
  for file in os.listdir('c:/py/tut/data/'):
    if fnmatch.fnmatch(file, '*.txt'):
        print(file) = print all files with txt extension in data subfolder

File Stats

The time when the file was accessed, created or modified are written to each file. File size and properties are also included in the file stats. To access this information, you need certain functions from the os.path module.

last time file was accessed

  from  datetime import *
  at = os.path.getatime("c:/py/tut/data/file.txt") = 1517755649.4889567
  datetime.fromtimestamp(at).strftime('%Y-%m-%d %H:%M:%S') ='2018-02-04 14:47:29'

time when the file was created

  from datetime import *
  ct = os.path.getctime("c:/py/tut/data/file.txt") =creation time
  datetime.fromtimestamp(ct).strftime('%Y-%m-%d %H:%M:%S'))

All file stats

These include file size, access time, modification time, creation time..etc. (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)

  size, atime, mtime, ctime, mode

Check file size


How to truncate file after number of bytes

import os
with open("c:/test.txt", "r+") as f:
  f.write("Hello World\nHello World\nHello World")
  print os.path.getsize("c:/test.txt")
open("c:/test.txt", "r").read()

Working with File Path

Certain information about file path and properties may be essential to a programmer. For example, you may need to know :

To get directory path without filename

  os.path.dirname("c:/scripts/tut/file.txt") ==> 'c:/scripts/tut'

To split path to filename and directory path

  os.path.split("c:/scripts/tut/file.txt")    ==> ('c:/scripts/tut', 'file.txt')

To get extension of filename from its path


To get full file path without its extension

  import os
  os.path.splitext("c:/py/tut/data/file.txt") =('c:/py/tut/data/file', '.txt')

To get the full file path without filename

  import os
  os.path.split("c:/py/tut/data/file.txt") =('c:/py/tut/data', 'file.txt')

To get file basename from full path

  from os.path import basename
  basename("c:/py/tut/data/file.txt")   ==> file.txt 
  basename("c:/py/tut/data/file.txt")[-3:]      =txt
  basename("c:/py/tut/data/file.txt")[:-4]      =file

To join parts of path into full path

  os.path.join("c:\script", "tut", "data")

Back to Top