1

I have such dir tree:

dir1
 - dir2
 - dir3
 - file1.java
 - file2.java
 - file3.cpp

and I want to move only *.java files to brand new directory called 'java_files' in current directory. How to make it? I read shutil doc but ...

This:

def moveFiles() :
    source = "."
    destination = "java_files"
    if os.path.isfile(source):
        shutil.move(source, destination)

doesn't work at all ...

I don't want to use shells 'mv' instead !

EDIT:

I also tried this:

def moveFiles() :
    source = "."
    destination = "java_files"
    dirList = os.listdir(source)
    for fname in  dirList:
        if fname.endswith(".java") :
            shutil.move(source, destination)

but it gives:

File "test.py", line 95, in <module>
 main()   File "test.py", line 91, in main
 moveFils()   File "test.py", line 82, in move
 shutil.move(source, destination)   File "/usr/lib/python2.7/shutil.py", line 295, in move
 raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst) shutil.Error:Cannot move a directory '.' into itself 'java_files'.
1
  • 2
    But what? Are you unable to write a check if the file name ends with .java? Commented Aug 20, 2012 at 15:20

2 Answers 2

2

In this kind of cases, I use glob (which accepts wildcards and regular expressions)

#!/usr/bin/env python

import glob
import shutil
import os

source="."
destination = "/tmp/newDestination/"

if not(os.path.exists(destination)):
    os.makedirs(destination)
elif not(os.path.isdir(destination)):
    raise OSError("Kabooom! The destination %s exists, but is not a directory" % destination)

for javaFile in glob.glob(os.path.join(source, "*.java")):
    if os.path.isfile(javaFile):
        shutil.move(os.path.abspath(javaFile), destination)
Sign up to request clarification or add additional context in comments.

8 Comments

when I have as source="." and as destination="./java_files" the code you gave creates only a FILE called java_files ...
That's because java_files is not a directory, is it? (or at least doesn't exist)... It's behaving as a mv foo /tmp/bar If bar doesn't exist, it'll move the file foo to a file called bar in /tmp/, whereas if bar exists and it's a directory, it'll put the file foo into the bar directory. I'll be right back :D
thanks for the answer! Is that mean that I have to create java_files dir first? ok, no prob!:)
my code looks like that now: pastie.org/private/kl2tnv4wyiczy8afmhzka and it's working perfectly, thanks! ;* is that possible, to move to java_files also dir2 and dir3 from my dir tree? How to do it?
|
2
if source.endswith('.java'):
    # do copy or move file

3 Comments

if os.path.isfile(fname) and fname.endswith('.java'):
Nobody can guess what you have done in addition - unless you provide the right code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.