ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi I was/am learning python and I was trying to make a program that will be able to read a k3b playlist file and print the output to a word document such as abiword, openoffice etc.
The *.k3b filetype is a sort of zip file containing 2 files maindata.xml and mymetypes.
How can I unzip/open a file in a python program. Here is my current rudimentary program.
Quote:
#!/usr/bin/python
import xml # hoping this will allow me to read xml files
playlist_file = open(raw.input("Open which k3b playlist?: "))
# this is where I need to open .k3b file to xml file
reader = xml.reader(playlist_file)
for line in reader:
print line # will make open with abiword or other when rest working
Thanks, I guess the problem I am having is getting my mind around in one process is getting the raw input and double opening (zip, read xml) in one process. So Do i have to put raw.input into the first open and then reaccess the xml file through a new open command, is that the best way ?
Thanks, I guess the problem I am having is getting my mind around in one process is getting the raw input and double opening (zip, read xml) in one process. So Do i have to put raw.input into the first open and then reaccess the xml file through a new open command, is that the best way ?
do it systematically
Code:
choice=raw_input("Ask user to enter file name here:")
## check user input code here
## if everything ok, open the file
f = open(choice) # r,w,a etc etc
...
..
but its up to you. anyway, from above, playlist_file is a file handle.
so this:
Quote:
Code:
os.system("cd /tmp && unzip " "playlist_file");
i don't understand. you can't unzip a file handle. to zip/unzip files in python, you can use
the gzip/zipfile modules. check the Python docs for more info on these 2 modules.
To change directory, use os.chdir("/tmp"). you should really learn how Python works by reading the relevant docs first.
Quote:
Code:
playlist_data = "/tmp/maindata.xml"
for line in playlist_data:
print line
playlist_file.close()
you defined playlist_data as xml file, and to print the contents of that xml file, you
have to open it, like this
Thanks for the reply I do appreciate the effort. I am reading "Beginning Python" by Hetland the Python.org docs and stuff I find on the net. Was actually trying to do a few small projects to reinforce what I am reading as for me anyway I was over halfway in the Hetland book and had a lot of theory but no practical so I was starting to get confused or overwhelmed. Thats why I really enjoyed this website http://programming-crash-course.com/ (It is very Hands on).
I got that unzip example from devshed
Quote:
import os
# unzip the file to /tmp/
os.system("cd /tmp/ && unzip " + zip_location);
try to refrain from shelling out to call commands from within python wherever possible as it is not portable way of coding. Python comes with modules that can perform OS functions. for that os.system() eg you gave, you can use the gzip/zipfile module as i have mentioned.
As you are learning python, i recommend the Python documentation site. you have everything you need there. Also take a look at the tutorial.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.