zip - proper way for zipping files in python -


i'm trying create zip file zipping couple text files specific directory. code looks following:

import zipfile,os  project='c:/users/user1/documents/work/filestozip'  dirlist = os.listdir(project) print dirlist zip_name = zipfile.zipfile(os.path.join(project,'jobs.zip'),'w') file in dirlist:     zip_name.write(os.path.join(project,file))  zip_name.close() 

the code runs fine , creates zip file, problem when open zip file found whole directory structure zipped. i.e. when open file find users open user1 open documents open work filestozip find files want zip. question how can red of file structure when open zip file find files zipped right away?

thanks in advance!

zipfile.write has an optional second parameter archname want.

import zipfile,os  project='c:/users/user1/documents/work/filestozip'  # prevent adding zip if old zip left in directory zip_path = os.path.join(project,'jobs.zip') if os.path.exists(zip_path):     os.unlink(zip_path);  dirlist = os.listdir(project)  zip_file = zipfile.zipfile(zip_path, 'w') file_name in dirlist:     zip_file.write(os.path.join(project, file_name), file_name) zip_file.close()