0

I have a script that works (kind of) but only if ran when you are currently in the directory path

directoryChosen = sys.argv[1]




for f in os.listdir(directoryChosen):
    fname, fext = os.path.splitext(f)
    if not f.startswith('.'):
        print f

        print fname + " Is fname"
        dest_path = fname
        print dest_path + " is dest path"
        print fname + "this is fname"
        if os.path.isdir(fname):
            print "Ended here/ Start work here"
            shutil.copy2(directoryChosen + '/' + f, dest_path)
        if not os.path.isdir(fname):
            print "working here kind of..lets make directories"

            os.mkdir(dest_path)
            print " Path is created"

            shutil.copy2(directoryChosen + f, dest_path)

Works If I run

 python /Users/eeamesX/PycharmProjects/Workmain/groupFiles.py continuous/

2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd.csv
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd Is fname
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd is dest path
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6ddthis is fname
working here kind of..lets make directories
 Path is created
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd.wav
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd Is fname
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd is dest path
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6ddthis is fname
Ended here/ Start work here
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd.xml
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd Is fname
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd is dest path
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6ddthis is fname
Ended here/ Start work here

doesnt work

python groupFiles.py /Users/eeamesX/work/data/GERMANY/DE_026/continuous/

2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd.csv
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd Is fname
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd is dest path
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6ddthis is fname
Ended here/ Start work here
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd.wav
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd Is fname
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd is dest path
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6ddthis is fname
Ended here/ Start work here
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd.xml
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd Is fname
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6dd is dest path
2015-06-01#e3cea6c4-07f2-4b84-985d-5ecabae9a6ddthis is fname
Ended here/ Start work here
 grouped the files boss  

I want it to create the directory if it is run both ways! Any help?

1
  • Can you write a new script with just two or three lines which also exhibits the same problem? See mcve Commented Oct 7, 2015 at 20:50

1 Answer 1

3

The names returned by os.listdir don't include the directory prefix. So every function that takes a filename requires you to concatenate the directory with the name. E.g.

    if os.path.isdir(directoryChosen + '/' + fname):
        print "Ended here/ Start work here"
        shutil.copy2(directoryChosen + '/' + f, directoryChosen + '/' + dest_path)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.