【问题标题】:os.chdir working once, then not working after called a second time; python scriptos.chdir 工作一次,然后在第二次调用后不工作;蟒蛇脚本
【发布时间】:2016-06-09 23:49:39
【问题描述】:

在以下脚本中,我尝试克隆除两个之外的所有项目,然后将这两个克隆到主路径,而不是我的项目目录:

#!/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)中,为什么这些项目仍然有自己的想法要克隆到项目目录中?谢谢

【问题讨论】:

  • 脚本没有为os.getcwd 显示错误的目录。你 chdir 到 HOMEPATH (改变当前的工作目录),这就是它打印的内容。
  • 正确。让我解决问题
  • `clone_project` 的代码在哪里?

标签: python linux shell python-3.x ubuntu


【解决方案1】:

猜测一下,你是不是改成相对路径而不改回来?

例如,如果您从

 /home/myuser

然后chdir进入project1

/home/myuser/project1

如果你然后尝试 chdir 进入 project2

/home/myuser/project1/project2

你可能是想先 chdir 回到起点。

【讨论】:

    【解决方案2】:

    问题出在您的clone_project 函数中。我的猜测(因为您没有发布该代码)是 projects 要么直接硬编码,要么设置为 PROJECT_PATH

    【讨论】:

    • 好几个月没看clone_projects了,谢谢
    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    • 2013-11-11
    相关资源
    最近更新 更多