Member Avatar for sravan953

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:

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

Is there a way I can do this?

Thanx in advance! ;)

Here's one way:

open_file=open('html.html','r')
file_lines=open_file.readlines()
file = file_lines[0].strip()  # First Line
loc_file = file_lines[1].strip()  # Second Line

HTH

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)

print(line1)
print(line2)

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, networking, learning, and sharing knowledge.