gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:48:59 29 4
gpt4 key购买 nike

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

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”。有什么想法吗?

谢谢!

最佳答案

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

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())
<小时/>

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

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())

关于python - python中压缩文件夹的完整文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7852539/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com