Python |太多级别的符号链接

时间:2020-03-16 17:23:11

标签: python operating-system scandir google-cloud-shell listdir

通过GCP Shell运行以下脚本:

/home/$USER/proj1/venv/bin/activate

我收到以下错误:

def unpack_model(model_path, uploaded_filename):
    """ unpack uploaded zip/rar file """
    name, file_type = os.path.splitext(uploaded_filename)
    filename = "model{}".format(file_type)
    os.rename(os.path.join(model_path, uploaded_filename),
              os.path.join(model_path, filename))
    Archive(os.path.join(model_path, filename)).extractall(model_path)
    os.remove(os.path.join(model_path, filename))
    if 'main.py' not in os.scandir(model_path):
        copydata(os.path.join(model_path, name), model_path)
        shutil.rmtree(os.path.join(model_path, name))

我尝试过os.listdir()给我同样的错误。当脚本在我的本地计算机上运行时,一切正常。

有人可以帮助解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您可以通过follow_symlinks = False忽略符号 示例:

def get_directory_size(self, directory):
    total_size = 0
    for entry in os.scandir(directory):
       if entry.is_file():
          total_size += entry.stat(follow_symlinks=False).st_size
       elif entry.is_dir(follow_symlinks=False):
          total_size += get_directory_size(entry.path)
    return total_size