os.chdir工作一次,之后再打不工作; python脚本

在下面的脚本中,我尝试克隆除了两个以外的所有项目,然后将这两个项目克隆到homepath中,而不是我的项目dir:

#!/usr/bin/env python import os, sys, subprocess, time, re from my_scripting_library import * bucket_hoss = BitbucketAPIHoss() all_project_names = bucket_hoss.get_bitbucket_project_names() print(all_project_names) os.chdir(PROJECT_PATH) print(PROJECT_PATH) TOP_LEVEL_PROJECTS = ['scripts', 'my_documents'] for project in all_project_names: if project not in TOP_LEVEL_PROJECTS: clone_project(project=project) os.chdir(HOMEPATH) print (HOMEPATH) print(os.getcwd()) for project in TOP_LEVEL_PROJECTS: clone_project(project=project) 

产量

 cchilders:~/scripts [master]$ ./git_and_bitbucket/clone_all.py [u'autohelper', u'bookwormbuddy', u'buildyourownlisp_in_C', u'bytesized_python', u'craigslist', u'foodpro', u'govdict', u'javascript-practice', u'learn_c_the_hard_way', u'my_documents', u'neo4j_sandbox', u'notes_at_work', u'poker_program_demo', u'portfolio', u'scriptcity_demo', u'scripts', u'transmorg_django', u'writing'] /home/cchilders/projects fatal: destination path '/home/cchilders/projects/autohelper' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/bookwormbuddy' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/buildyourownlisp_in_C' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/bytesized_python' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/craigslist' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/foodpro' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/govdict' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/javascript-practice' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/learn_c_the_hard_way' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/neo4j_sandbox' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/notes_at_work' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/poker_program_demo' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/portfolio' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/scriptcity_demo' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/transmorg_django' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/writing' already exists and is not an empty directory. /home/cchilders /home/cchilders fatal: destination path '/home/cchilders/projects/scripts' already exists and is not an empty directory. fatal: destination path '/home/cchilders/projects/my_documents' already exists and is not an empty directory. 

既然脚本现在显示在正确的目录(我的HOMEPATH),为什么这些项目仍然有他们自己的想法克隆到项目目录? 谢谢

问题在你的clone_project函数中的某处。 我的猜测(因为你没有发布代码)是projects是直接硬编码,或设置为PROJECT_PATH

猜测,你是不是变成了一个相对的路径,而不是变回来?

例如,如果你开始

  /home/myuser 

然后chdir到project1

 /home/myuser/project1 

如果你然后尝试chdir到project2

 /home/myuser/project1/project2 

您可能首先想要回到起点。