Tutorial by Examples

There are different modes you can open a file with, specified by the mode parameter. These include: 'r' - reading mode. The default. It allows you only to read the file, not to modify it. When using this mode the file must exist. 'w' - writing mode. It will create a new file if it does no...
The simplest way to iterate over a file line-by-line: with open('myfile.txt', 'r') as fp: for line in fp: print(line) readline() allows for more granular control over line-by-line iteration. The example below is equivalent to the one above: with open('myfile.txt', 'r') as fp: ...
The preferred method of file i/o is to use the with keyword. This will ensure the file handle is closed once the reading or writing has been completed. with open('myfile.txt') as in_file: content = in_file.read() print(content) or, to handle closing the file manually, you can forgo with...
with open('myfile.txt', 'w') as f: f.write("Line 1") f.write("Line 2") f.write("Line 3") f.write("Line 4") If you open myfile.txt, you will see that its contents are: Line 1Line 2Line 3Line 4 Python doesn't automatically add line b...
with open(input_file, 'r') as in_file, open(output_file, 'w') as out_file: for line in in_file: out_file.write(line) Using the shutil module: import shutil shutil.copyfile(src, dst)
Employ the EAFP coding style and try to open it. import errno try: with open(path) as f: # File exists except IOError as e: # Raise the exception if it is not ENOENT (No such file or directory) if e.errno != errno.ENOENT: raise # No such file or directory ...
import shutil source='//192.168.1.2/Daily Reports' destination='D:\\Reports\\Today' shutil.copytree(source, destination) The destination directory must not exist already.
To iterate all files, including in sub directories, use os.walk: import os for root, folders, files in os.walk(root_dir): for filename in files: print root, filename root_dir can be "." to start from current directory, or any other path to start from. Python 3.x3.5 If ...
So let's suppose you want to iterate only between some specific lines of a file You can make use of itertools for that import itertools with open('myfile.txt', 'r') as f: for line in itertools.islice(f, 12, 30): # do something here This will read through the lines 13 to 20 as i...
Using the mmap module allows the user to randomly access locations in a file by mapping the file into memory. This is an alternative to using normal file operations. import mmap with open('filename.ext', 'r') as fd: # 0: map the whole file mm = mmap.mmap(fd.fileno(), 0) # print ...
import fileinput replacements = {'Search1': 'Replace1', 'Search2': 'Replace2'} for line in fileinput.input('filename.txt', inplace=True): for search_for in replacements: replace_with = replacements[search_for] line = line.replace(search_for, replace_with...
>>> import os >>> os.stat(path_to_file).st_size == 0 or >>> import os >>> os.path.getsize(path_to_file) > 0 However, both will throw an exception if the file does not exist. To avoid having to catch such an error, do this: import os def is_empty_...

Page 1 of 1