Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

python - file not found error but showing contents of file in error


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I wasnt able to reproduce your error, best I could to mimic your code is posted below

not sure is doing what you expected from your code


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import random


with  open("songs.txt","r") as filesongs: 
            songs=filesongs.read()
            ix=0
            for i in songs:
                if i =='
':
                    ix += 1
                else:
                    ix=ix
print('
number of lines in songs.txt : ',ix)
                
filesongs.close()

random=random.randint(0,ix)
print('
random number betwen 0 and lines in songs.txt : ',random)
with open("songs.txt","r") as ransong:

    pippo  = []
    
    for i in ransong.readlines():
         i=i.replace('
','')
         pippo += i
        
    
#    print('
all the lines in songs.txt as list : ',pippo)
           
    
    print('
random line in songs.txt : ',pippo[random])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...