Re: Help please with code to find and move files.



On Dec 31, 2:44 pm, inFo...@xxxxxx wrote:
On Sun, 30 Dec 2007 19:29:38 -0800 (PST), John Machin





<sjmac...@xxxxxxxxxxx> wrote:
On Dec 31, 1:04 pm, inFo...@xxxxxx wrote:
Hello,

I am new to python and wanted to write something for myself where
after inputing two words it would search entire drive and when finding
both names in files name would either copy or move thoe files to a
specified directory.

But couple of attempts did not work as desired this is one of them.

Care to provide some more details than "did not work as desired"? Do
you think the problem is in the finding or in the copying? I've given
some comments below, but you really need to think through what "as
desired" means ...

Suppose your search words are "foo" and "bar", that C:\files is an
empty folder, and the following 3 files exist:
C:\a\foobar.txt
C:\b\foobar.txt
C:\b\barfoo.txt

What do you want to happen the first time you run the script? ... if
you run it a second time? If it's your intention not to make a copy of
C:\b\foobar.txt (because its "basename" is the same as that of C:\a
\foobar.txt), consider the desirability of warning yourself when this
situation happens.

Could someone help fix it or maybe give a better example.

 Thank you very much.

import os, os.path, shutil

path = r"c:\\"

Leave out the "r"; you are getting TWO backslashes:

path = r"c:\\"
len(path)
4
import os
wkr = os.walk('rd:\\')
wkr.next()
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
StopIteration
# Nothing inside your for statement would be executed
wkr = os.walk('d:\\')
wkr.next()
('d:\\', a list of folders, a list of files)

dest_file = 'C:\\files'

Presumably that would be better named dest_dir ...

name_search = raw_input('Please enter name searchs : ').split()
dup = []

In the (unlikely) event that an in-memory structure with no knowledge
of what happened on previous runs will do what you really want to do,
then consider a set instead of a list.

for root, dirs, files in os.walk(path):
    for name in files:
                file_name = os.path.join(root, name)
                if (name_search[0] in file_name) and (name_search[1]
in file_name):
                        #if os.path.join(root, name) in dest_file:
                        if file_name in dup:

What do you really intend to do here? dup contains the FULL PATH of
each file that you have found; if you come across another instance of
one of those, either os.walk is horribly broken or your filesystem has
a loop in its directory structure.

If you really mean "am I about to try to copy over the top of an
existing file", attack the problem head-on: make the full path of the
file you are about to try to create, and use os.path.exists on it.

                                break

Why break?

You also want to avoid trying to copy files in the backup
("dest_file") directory, perhaps including ones that you have just
copied there. Try a simple test
   if root == dest_file:
       continue
very early in your outer loop. It's probably a good idea to wrap
os.path.abspath() around root and destfile.

                        else:
                                print "copied %s to %s" % (name,
dest_file)
                                shutil.copy(os.path.join(root, name),
dest_file)

You may prefer the results of copy2 to those of copy.

                                dup.append(file_name)

HTH,
John

John,

What I was trying to do is find files that are scattered all over my
hard drive that contain similar two words in them like bar and foo
and move them to one desired location removing from where they were
originally.  The did not work as desired were attempts when it would
attempt to read and write to the same location.so i would get an error
saying that source and destination were  the same.- Hide quoted text -

- Show quoted text -

The script that you showed would not have found any files to move/
copy, as "infixum" and I have pointed out.

Imagine that you were trying to help someone with a Python problem ...
would you not like them to tell you (with some precision) what they
were trying to do, what was the script that they actually ran, what
the precise result (including stack trace and error message if any)
was? Or do you like playing guessing games?
.



Relevant Pages

  • Re: Help please with code to find and move files.
    ... very early in your outer loop. ... The script that you showed would not have found any files to move/ ... would you not like them to tell you (with some precision) what they ... the precise result (including stack trace and error message if any) ...
    (comp.lang.python)
  • Re: Form Security
    ... After all this, if no error message has been generated, the form contents are emailed to me. ... I'm no Linux guru, so I don't know what someone could do to cause problems with this script, other than spam me. ... What he's proposing is false security - which is worse than no security ...
    (comp.lang.php)
  • RE: Cannot Edit Logion Scripts
    ... remove the script that you would like to edit. ... Please capture a screenshot of the error message received on the client ... Microsoft Online Newsgroup Support ... This newsgroup only focuses on SBS technical issues. ...
    (microsoft.public.windows.server.sbs)
  • Re: Error in script of Internet Explorer
    ... Uncheck the box to Display a notification about every script error. ... Windows Script 5.6 for Windows 2000 and XP ... Error Message When You Browse the Web: An Error Has Occurred in the Script ... Please reply to the newsgroup so others may benefit. ...
    (microsoft.public.windows.inetexplorer.ie6.browser)
  • Re: Sound file
    ... There shouldn't be any problems in the limited user accounts. ... The only other problem just about has to be with the attaching of the .wav ... > Common script errors messages can be eliminated by Clicking: ... >> click on the html file we get an error message and the wav file does ...
    (microsoft.public.windowsxp.accessibility)