python中压缩文件夹的完整文件路径

时间:2011-10-21 16:39:47

标签: python file path zip

我有以下代码试图获取完整的文件路径(包括文件夹):

import zipfile
import os
import sys

zipped_files_dir = 'Z:\Dev\some_files'

def get_folder_names():
    path_list = []
    for folder_name in os.listdir(zipped_files_dir):
        path_list.append(folder_name)
    return path_list

def get_folder_directories(folder_list):
    for folder in folder_list:
        pathname = os.path.abspath(folder)
        print(pathname)

def main():
    get_folder_directories(get_folder_names())

>>>Z:\Dev\new_folder.zip  

我的问题是我应该在返回的目录中有“\ some_files \ new_folder.zip”。有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

您可以在os.path.join(zipped_files_dir, folder)函数中使用get_folder_directories

import zipfile
import os
import sys

zipped_files_dir = 'Z:\Dev\some_files'

def get_folder_names():
    path_list = []
    for folder_name in os.listdir(zipped_files_dir):
        path_list.append(folder_name)
    return path_list

def get_folder_directories(folder_list):
    for folder in folder_list:
        pathname = os.path.abspath(os.path.join(zipped_files_dir, folder))
        print(pathname)

def main():
    get_folder_directories(get_folder_names())

path_list.append(os.path.join(zipped_files_dir, folder_name))中的get_folder_names()

import zipfile
import os
import sys

zipped_files_dir = 'Z:\Dev\some_files'

def get_folder_names():
    path_list = []
    for folder_name in os.listdir(zipped_files_dir):
        path_list.append(os.path.join(zipped_files_dir, folder_name))
    return path_list

def get_folder_directories(folder_list):
    for folder in folder_list:
        pathname = os.path.abspath(folder)
        print(pathname)

def main():
    get_folder_directories(get_folder_names())

答案 1 :(得分:1)

Chown有正确的解决方案。

在你的代码中,你传递abs_path字符串“new_folder.zip”。但是abs_path不知道它来自哪里,所以它认为它必须在当前的工作目录中,这就是你得到r“Z:\ Dev \ new_folder.zip”的原因。您需要使用os.path.join将文件名与您在其中找到的路径组合在一起。