Python脚本仅在从目录运行时才有效

时间:2015-10-07 20:45:33

标签: python

我有一个可以工作的脚本(种类),但只有当你当前在目录路径中时才会运行

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)

作品 如果我跑

 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

无效

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  

我希望它创建目录,如果它是双向运​​行的!有什么帮助吗?

1 个答案:

答案 0 :(得分:3)

os.listdir返回的名称不包含目录前缀。因此,每个带文件名的函数都要求您将目录与名称连接起来。 E.g。

    if os.path.isdir(directoryChosen + '/' + fname):
        print "Ended here/ Start work here"
        shutil.copy2(directoryChosen + '/' + f, directoryChosen + '/' + dest_path)