0

I want the code to generate a random integer then pick out a random line from an external folder

I have tried referring it as filesongs but it gives me a textIOwrapper error

Can anyone explain why my code is saying its not finding the folder however is showing me the contents of said folder?

with  open("songs.txt","r") as filesongs: 
            songs=filesongs.read().replace('\n', '')

random=random.randint(0,3)
ransong=open("songs.txt","r").readline(0,random)

Error Traceback:

FileNotFoundError: [Errno 2] No such file or directory: 'song one song two song three song four '
DapperDuck
  • 2,728
  • 1
  • 9
  • 21
cakemate
  • 11
  • 1
  • Please update your question with the full error traceback. – quamrana Jan 02 '21 at 16:30
  • need to close the fie before reopening – pippo1980 Jan 02 '21 at 16:34
  • TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper – cakemate Jan 02 '21 at 16:35
  • Sorry, I meant a *full* error traceback, the one which quotes part of your script, along with the error message. Use the [edit](https://stackoverflow.com/posts/65541371/edit) button to edit it into your question. – quamrana Jan 02 '21 at 16:36
  • \Documents\comperter project\actual code.py", line 19, in ransong=open(filesongs,"r").readline(0,random) TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper – cakemate Jan 02 '21 at 16:43
  • Please update your question with your *real* code. Your posting and your error traceback do not match. – quamrana Jan 02 '21 at 16:48
  • Ok, be sure to check this answer https://stackoverflow.com/a/55687657/152016 I prefer it over the accepted answer. – Niloct Jan 02 '21 at 17:10

1 Answers1

0

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 =='\n':
                    ix += 1
                else:
                    ix=ix
print('\nnumber of lines in songs.txt : ',ix)
                
filesongs.close()

random=random.randint(0,ix)
print('\nrandom 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('\n','')
         pippo += i
        
    
#    print('\nall the lines in songs.txt as list : ',pippo)
           
    
    print('\nrandom line in songs.txt : ',pippo[random])
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
pippo1980
  • 2,181
  • 3
  • 14
  • 30