5.4. Using a `Node`'s File Name as a String
Prev | Chapter 5. Node Objects | Next

## 5.4. Using a `Node`'s File Name as a String

Printing a `Node`'s name as described in the previous section works because the string representation of a `Node` object is the name of the file. If you want to do something other than print the name of the file, you can fetch it by using the built-in Python `str` function. For example, if you want to use the Python `os.path.exists` to figure out whether a file exists while the `SConstruct` file is being read and executed, you can fetch the string as follows:

    import os.path
    program_list = Program('hello.c')
    program_name = str(program_list[0])
    if not os.path.exists(program_name):
        print("%s does not exist!"%program_name)

Which executes as follows on a POSIX system:

    % scons -Q
    hello does not exist!
    cc -o hello.o -c hello.c
    cc -o hello hello.o

Prev | Up | Next
5.3. Printing `Node` File Names | Home | 5.5. `GetBuildPath`: Getting the Path From a `Node` or String