Member Avatar

I have a file, called 'html.html', which contains 'open_skype' in the first line and in the second line it contains the location of the file.

Now, what I want to do is, make a function read the first line and then the second line should be read separately....if you don't understand what I'm trying to say:

#I want loc_file to read ONLY the second line, and file to read ONLY the first line
if file=='shutdown':
    os.system('shutdown -s -t 10')
elif file=='open_tweetdeck':'C:\Program Files\TweetDeck\TweetDeck.exe')
elif file=='open_skype':
    print 'No command given...'
    raw_input('Press <enter>')

Is there a way I can do this?

Thanx in advance! ;)

Recommended Answers

All 2 Replies

Here's one way:

file = file_lines[0].strip()  # First Line
loc_file = file_lines[1].strip()  # Second Line


NOTE: Using file for a variable name is not recommended as it's a reserved word in Python... I'd use something else... maybe something that's descriptive.

Firstly note that 'file' is a Python function name and should not be used for a variable name!

Python has module linecache to do what you so desire ...

import linecache

filename = "html.html"
# read a selected line (first line is 1)
# note that each line will have a trailing '\n'
line1 = linecache.getline(filename, 1)
line2 = linecache.getline(filename, 2)


Or you can do what jlm699 so kindly recommended.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.