Note that there are some explanatory texts on larger screens.

plurals
  1. POselect multiple files. python
    primarykey
    data
    text
    <p>I have created this search and replace program. But I want to make changes to it, so I can do a search and replace for multiple files at once. Now, is there a way so I have the option to select multiple files at once from any folder or directory that I choose.</p> <p>The code that helps me to select files using file dialog window is given below, but is giving errors. can you help me to correct it? The FULL traceback error is :</p> <pre><code>Traceback &lt;most recent call last&gt;: File "replace.py", line 24, in &lt;module&gt; main() File "replace.py", line 10, in main file = tkFileDialog.askopenfiles(parent=root,mode='r',title='Choose a file') File "d:\Python27\lib\lib-tk\tkFileDialog.py",line 163, in askopenfiles ofiles.append(open(filename,mode)) IOError: [Errno 2] No such file or directory: u'E' </code></pre> <p>And here's the code: I finally got this code to work I changed 'file' to 'filez' and 'askopenfiles' to askopenfilenames'. and I was able to replace the word in my chosen file. the only thing is that it doesnt work when I choose 2 files. maybe I should add in a loop for it to work for multiple files. But, this was a kind of trial and error and I want to be able to really know why it worked. Is there a book or something that will help me to fully understand this tkinter and file dialog thing? anyways, I have changed the code below to show the working code now:</p> <pre><code>#replace.py import string def main(): #import tkFileDialog #import re #ff = tkFileDialog.askopenfilenames() #filez = re.findall('{(.*?)}', ff) import Tkinter,tkFileDialog root = Tkinter.Tk() filez = tkFileDialog.askopenfilenames(parent=root,mode='r',title='Choose a file') #filez = raw_input("which files do you want processed?") f=open(filez,"r") data=f.read() w1=raw_input("what do you want to replace?") w2= raw_input("what do you want to replace with?") print data data=data.replace(w1,w2) print data f=open(filez,"w") f.write(data) f.close() main() </code></pre> <p>EDIT: One of the replies below gave me an idea about file dialog window and now I am able to select multiple files using a tkinter window, but I am not able to go ahead with the replacing. it's giving errors. I tried out different ways to use file dialog and the different ways are giving different errors. Instead of deleting one of the ways, I have just put a hash sign in front so as to make it a comment, so you guys are able to take a look and see which one would be better.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload