[Tutor] HELP: Creating animation from multiple plots

Peter Otten __peter__ at web.de
Wed Mar 27 17:36:52 CET 2013


Sayan Chatterjee wrote:

> for t in range(0,200):
>   fname = 'file_' + str(t)
> 
> So it will assign fname values file_0, file_1 so on. Dropping the quotes
> is giving me
> 
> IOError: [Errno 2] No such file or directory: 'file_0'
> 
> 
> Indeed the file is not present. In C we write,if we have to record data in
> a file
> 
> FILE *fp
> 
> fp = fopen("file.dat","w")
> 
> 
> Here I want to write different data sets in files having different name
> i.e I want to create the files with the data sets. I am quite new to
> Python, so you can assume zero knowledge while answering. Thanks for your
> support. :)

If you try to open a non-existent file in "r+" mode in C you should get an 
error, too. The following C code

FILE * f;
int i;
char filename[100];

for (i=0; i<10; i++) {
    sprintf(filename, "foo%d.dat", i);
    FILE * f = fopen(filename, "w");
    /* write stuff to file */
    ...
    fclose(f);
}

translates into this piece of Python:

for i in range(10):
    filename = "foo%d.dat" % i
    with open(filename, "w") as f:
        # write stuff to file
        ...




More information about the Tutor mailing list