1

I have a string path "path=D:/projects/file.ext". Is there a way to convert this string to class and add to it a method which will do something with file.ext? The file could be any type Does not matter. For example:

my_file="D:/projects/file.ext"
obj = StrToObj(my_file)
obj.do_something_with_file()

2 Answers 2

2

I think you can follow this template:

class StrToObj:
    def __init__(self, name, ...):
        self.name = name
        ...
    def do_something_with_file(self, ...):
        ...
Sign up to request clarification or add additional context in comments.

1 Comment

This isn't doing anything with the file. Just uses the string.
0

This is an odd situation, but here is how I interpret your question:

# Define object
class obj:
     def __init__(self, file):
          self.file = file
# function to open file and pass it to a class
def strToObj(name):
     with open(name, "r") as f:
          obj_1 = obj(f)
     return obj_1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.