LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-01-2007, 06:16 AM   #1
flebber
Member
 
Registered: Jan 2005
Location: Newcastle, Australia
Distribution: debian stable
Posts: 394

Rep: Reputation: 30
python program to read k3b playlist


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

playlist_file.close()
 
Old 06-01-2007, 09:39 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by flebber
How can I unzip/open a file in a python program.
to unzip/zip files , see here
to open a file, use the open() file method. See the official python docs or a random site here
 
Old 06-02-2007, 08:47 PM   #3
flebber
Member
 
Registered: Jan 2005
Location: Newcastle, Australia
Distribution: debian stable
Posts: 394

Original Poster
Rep: Reputation: 30
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 ?
 
Old 06-02-2007, 09:03 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by flebber
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
...
..
 
Old 06-07-2007, 09:33 AM   #5
flebber
Member
 
Registered: Jan 2005
Location: Newcastle, Australia
Distribution: debian stable
Posts: 394

Original Poster
Rep: Reputation: 30
Quote:
#!/usr/bin/python

import os
playlist_file = open('/home/flebber/oddalt.k3b')
os.system("cd /tmp && unzip " "playlist_file");

playlist_data = "/tmp/maindata.xml"

for line in playlist_data:
print line

playlist_file.close()
That is my program so far however it doesn't read the file properly. This is all it is doing
Quote:
>>>
/
t
m
p
/
m
a
i
n
d
a
t
a
.
x
m
l
>>>
It is not reading the file only the filename

Last edited by flebber; 06-07-2007 at 09:45 AM.
 
Old 06-07-2007, 10:13 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Code:
import os
playlist_file = open('/home/flebber/oddalt.k3b')
i usually write
Code:
mypath = os.path.join("/home","flebber","oddalt.k3b")
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
Code:
for line in open(playlist_data):
    print line
do you really have to do this in Python?
 
Old 06-07-2007, 06:19 PM   #7
flebber
Member
 
Registered: Jan 2005
Location: Newcastle, Australia
Distribution: debian stable
Posts: 394

Original Poster
Rep: Reputation: 30
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);

maindata_location = "/tmp/maindata.xml"

# read the xml data

Last edited by flebber; 06-07-2007 at 06:21 PM.
 
Old 06-07-2007, 08:40 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rhythmbox- Writing a Program to Automate Playlist Creation RevenantSeraph Linux - Software 1 04-16-2007 02:39 AM
First Python Program dudeman41465 Programming 3 01-21-2006 02:22 PM
Old CDROM doesn't Read K3B Burned CD rvijay Linux - Software 1 02-10-2005 08:42 AM
python read output from seperate script? bendeco13 Programming 1 02-01-2005 10:38 PM
A Program/script making a html playlist? Trinity22 Linux - Software 4 04-13-2004 09:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration