Question: I am a beginner in python and am trying to copy a csv of 1 M records line by line to a new CSV using the Memory Map implementation.

But going by the rule that the mmap [module](https://docs.python.org/2/library/mmap.html) has it doesn't map to an empty csv file. But the problem that I have right now requires me to create a new csv file at the time of copying the dataset.

Can you guys suggest any pointers/help? Here is my code:

```
import os
import mmap
import time
import csv

def mmapUsage():
    start = time.time()
    with open("/home/delhivery/Documents/Python/dataset.csv","r+b") as f:
        mapInput = mmap.mmap(f.fileno(), 0)
        L = list()
        for i in iter(mapInput.readline, ""):
            L.append(i)
        print "List length: ", len(L)
        mapInput.close()
        end = time.time()
        print "Time for completion", end-start

if __name__ == "__main__":

    print "MMap Implementation"
    mmapUsage()

```

Answer: 
If I understand your question correctly (taken together with your additional comments), you are reading and parsing an input CSV file via mmap, and you have no problem with that aspect of your task. Your difficulty is in creating a new CSV file containing the input CSV data (possibly modified), i.e. writing the data back out - specifically using mmap techniques.

You can't mmap an empty file, but you can create a file, write some data to it, and then mmap it as shown here, which copies the input file to the output file:

```
import mmap

with open('dataset.csv', 'r+b') as f, open('outfile.csv', 'w+b') as outfile:
    map_input = mmap.mmap(f.fileno(), 0)
    outfile.write('any old thing will do')
    outfile.flush()                        # important, now you can mmap the file
    map_output = mmap.mmap(outfile.fileno(), 0)
    map_output.resize(map_input.size())    # resize out map to the same size as input file
    # copy all data from map_input to map_output
    # map_output[:] = map_input[:]         # this is the fastest way to copy
    for line in iter(map_input.readline, ''):
        map_output.write(line)
    map_output.close()
    map_input.close()

```

Note that if you are *not* modifying the input CSV data, then your task degenerates into a simple file copying exercise for which you should access OS level commands (e.g. `cp` in \*nix), or copy the file using something like [`shtuil.copy()`](https://docs.python.org/2/library/shutil.html#shutil.copy).

If you are modifying the incoming data, then you need to worry about the post-modification length of the data. You need to handle the case that the output data is larger than the input data and ensure that the underlying map is sufficiently large to handle all the data. After processing you can call `map_output.resize()` again with the final length of the data.

There is obviously a lot more to consider when using mmap, and by the time you have actually *parsed* the CSV data (which your code ignores), processed the data, converted back to CSV, and handled the data length issue present in mmap, you'd be better off just using the csv module. Performance-wise the csv module will be slower, but does your application really require faster performance at the cost of code complexity?
If you only want to perform *per line parsing*, you can handle very large files in standard Python as long as you do it one line at the time. I'm a bit unsure if that is what you want, but this code works fine on gigabyte size files:

```
infile='input.csv'
outfile='out.csv'

with open(infile,'r') as in_f, open(outfile,'w') as out_f:
    line_counter=0
    for line in in_f:
        # Perform some optional parsing
        line_counter+=1
        print >> out_f,line, # Write out your (parsed) line

print "%d lines in input file" % line_counter

```

This solution doesn't use mmap, but from your comment above, that wasn't really a requirement.