Reputation: 8297
def generate_log(dirname, log_object):
print(os.path.dirname(__file__),'dfsdfds')
print(os.listdir())
for smell in log_object:
log = open("../../output/logs/{}_logs".format(smell), "w")
for elem in log_object[smell]:
log.write('filename: {}, smelly_lines: {}, metric: {}\n'.format(elem['filename'], str(elem['lineno']), str(elem['metric'])))
My function tries to write some logs in to the directory output/logs/
The directory that I am calling this function is C:\Users\user\Desktop\proj\src\Detector
. Since I want the output
directory to be generated in /proj
which is my project root, I thought doing ../../
would work, but it gives me
log = open("../../output/logs/{}_logs".format(smell), "w")
FileNotFoundError: [Errno 2] No such file or directory: '../../output/logs/long_method_logs'
Is there anything I can do to fix this?
Upvotes: 0
Views: 57
Reputation: 267
You should use the os module with its submodule os.path
For joining a paths you should do os.path.join(path1, path2)
In your case you should do something like this:
log = open(os.path.join(os.pardir, os.pardir, "output", "logs", "{}_logs").format(smell), "w")
Also you should close
this file via log.close()
before exit
And for this purposes there is a logging module
Upvotes: 2