kebechet_200
kebechet_200

Reputation: 33

how to convert _io.TextIOWrapper to string? im getting always same error

When I run my code I get this error:

TypeError: stat: path should be string, bytes, os.PathLike or integer, not _io.TextIOWrapper

import csv
import os
from time import sleep

class Parser():
    def __init__(self):
        file = ''

    def fileLocation(self,file):
        with open(file) as f:
            if os.path.isfile(f) == True:
                print("Located Succesfully !")
            else:
                while os.path.isfile(f) == False:
                    print("Locate cant find, try again")
                    p = input("Enter correct location : ")
                    if os.path.isdir(p) == True:
                        print("Thanks god, u typed correctly")
                        break

file = input("Enter file location : ")
file = str(file)

def main():
    a = Parser()
    print("Im checking if folder exist...")
    sleep(0.5)
    a.fileLocation(file)

if __name__ == '__main__':
    main()

I tried to convert to a string, but the result is the same. Any solutions?

Upvotes: 1

Views: 2350

Answers (1)

wjandrea
wjandrea

Reputation: 33042

In your fileLocation, the file variable is the path to the file, not f. You can use file in the os.path methods. You don't need to open the file to do that.

Copied from khelwood's comment


To start:

def fileLocation(self, file):
    if os.path.isfile(file):
        ...

By the way, == True is redundant.

Upvotes: 1

Related Questions