dimanche 3 avril 2016

Python : How to choose a random file from a directory to write in

I want to choose a random file from a directory and write something in it. The problem is I don't know how to do it.

import os
import random

random_file = random.choice(os.listdir("E:\same")) #here the random file is for ex. text3.txt

open_file = open(random_file, "w") #but when I want to open it it creates a    new file because it doesn't know text3.txt's directory.
open_file.write("random")
open_file.close

so my question is how can I get a random file's directory to write in?? thanks in advance.




Aucun commentaire:

Enregistrer un commentaire