import os, tarfile, tempfile, shutil temp_dir = tempfile.mkdtemp() os.chdir(temp_dir) short_dir_path1 = 'a' short_dir_path2 = 'b/' long_dir_path1 = 'a' * 101 long_dir_path2 = ('b' * 101) + '/' os.mkdir(short_dir_path1) os.mkdir(short_dir_path2) os.mkdir(long_dir_path1) os.mkdir(long_dir_path2) tf = tarfile.open('test.tar', 'w') tf.add(short_dir_path1) tf.add(short_dir_path2) tf.add(long_dir_path1) tf.add(long_dir_path2) tf.close() tf = tarfile.open('test.tar') # For short names I need to omit the trailing slash tf.getmember(short_dir_path1) # This works, no trailing slash try: tf.getmember(short_dir_path2) # This fails due to trailing slash except KeyError: pass else: print "KeyError not raised" tf.getmember('b') # Omitting the trailing slash works # For long names I need to include the trailing slash try: tf.getmember(long_dir_path1) # Fails due to lack of trailing slash except KeyError: pass else: print "KeyError not raised" tf.getmember(long_dir_path1 + '/') # Works if I include it tf.getmember(long_dir_path2) # Also works since it has trailing slash # This is a work around for consistent behavior (only in 2.7, as 2.6 lacks # the 'normalize' keywork) tf._getmember(long_dir_path1, normalize=True) shutil.rmtree(temp_dir)