Total Pageviews

Popular posts


 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 in python indexing starts from 0. So line number 1 is indexed as 0

As can also read some extra lines by making use of the next() keyword here.

And when you are using the file object as an iterable, please don't use the readline() statement here as the two techniques of traversing a file are not to be mixed together 

 Copy a directory tree

import shutil source='//192.168.1.2/Daily Reports' destination='D:\\Reports\\Today' shutil.copytree(source, destination)  
The destination directory must not exist already. 

 Copying contents of one file to a dierent file
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)

No comments

ict note in A/L

3/Technology ict note in A/L/post-list