A TextCommand has a property named view that represents the file that the command is executing inside, and in turn the view has a method called window() that tells you what Sublime window the view is currently contained in.
As such, the line you want is self.view.window().project_file_name() to get at the information that you desire.
Note however that if you do this in a window that doesn’t have an associated project, it’s going to return None, which is probably not what you want.
I would recommend adding something like:
def is_enabled(self):
return self.view.window().project_file_name() is not None
That will make your command disable itself if you try to run it in a window without a project file associated. When a command is disabled it will appear grayed out in the menu, it won’t appear in the command palette (if you have added it there) and any key bindings for it also will not trigger.