InterviewSolution
| 1. |
Solve : Python 3 script? |
|
Answer» I need help writing a python script that will loop through a jar file and extract all files of a specified TYPE while keeping the file and directory names. Unless an "r" or "R" prefix is present, escape sequences in strings are interpreted according to rules similar to those used by Standard C.I had the 'r' in there and the error was the same. Current code: Code: [Select] import os import zipfile from zipfile import * zip = zipfile.ZipFile("C:\Users\Jordan.Office-PC\Desktop\BatPad.zip", 'a') names = zip.getinfo() print(names) zip.close()I don't see an 'r' in there. It goes before the string literal itself- eg r"C:\Users" rather than "C:\Users". You'll encounter a error where ZipFile.getinfo() takes two arguments and you are only passing one (well, none, but it gets self since it's an instance method). IF only there was somedocumentation on these methods and what they do, could save a lot of you simply googling how to do what you want and making a Frankenstein's monster out of the bits you find. You're probably after .namelist() here.I get the same error with 'r' in there. could be that I am on windows vista and need to find a way to run a py script as admin? P.S:your documentation is for 2.7.4, I am using 3.2.1Quote from: TheWaffle on April 27, 2013, 05:48:02 AM I get the same error with 'r' in there.Post the code you are actually using. Quote could be that I am on windows vista and need to find a way to run a py script as admin?Assuming the user folder you are trying to access is the one for the account being used, no. Quote P.S:your documentation is for 2.7.4, I am using 3.2.1There is no difference between the two that would affect this script. my notations on the getinfo() method are still accurate. I've tested my original implementation, which required a few FIXES: Code: [Select]#!/usr/bin/env python import os,sys, zipfile zip = zipfile.ZipFile(sys.argv[1], 'r') for name in zip.namelist(): #perform test condition here to see if name is a name you want to extract. #eg if name.endswith(".java"): if name.endswith(".java") print(name) zip.extract(name) zip.close() It works in all the versions of Python I tested in. Accepts filename as part of the commandline. Your hang-up is on making a string literal, and you are almost certainly doing something wrong. What, I can't say- I lack psychic debugging powers so I cannot actually see the script you are running. |
|