It might help if you can give some more details:
- How is that python code being run? Is it being run as an action?
- What is the
my_file
target?
- You mention that you have some rules. Are these Starlark rules that you wrote?
But to answer directly: When a binary is run with bazel run
, it's executed in a "runfiles tree" that contains symlinks of all the dependencies of the binary. So your file will be put in the binary's runfiles directory.
my_program.py
:
import os
from pathlib import Path
print("current working directory: " + os.getcwd())
p = Path('my_file.txt')
p.write_text('testing')
print("Testing done")
BUILD
:
py_binary(
name = "my_program",
srcs = ["my_program.py"],
)
$ bazel run my_program
INFO: Analyzed target //:my_program (18 packages loaded, 90 targets configured).
INFO: Found 1 target...
Target //:my_program up-to-date:
bazel-bin/my_program
INFO: Elapsed time: 0.226s, Critical Path: 0.01s
INFO: 5 processes: 5 internal.
INFO: Build completed successfully, 5 total actions
INFO: Build completed successfully, 5 total actions
current working directory: /home/ahumesky/.cache/bazel/_bazel_ahumesky/5123c9882cdbb6c5e34f583431173549/execroot/__main__/bazel-out/k8-fastbuild/bin/my_program.runfiles/__main__
Testing done
$ ls /home/ahumesky/.cache/bazel/_bazel_ahumesky/5123c9882cdbb6c5e34f583431173549/execroot/__main__/bazel-out/k8-fastbuild/bin/my_program.runfiles/__main__
external my_file.txt my_program my_program.py
$ cat /home/ahumesky/.cache/bazel/_bazel_ahumesky/5123c9882cdbb6c5e34f583431173549/execroot/__main__/bazel-out/k8-fastbuild/bin/my_program.runfiles/__main__/my_file.txt
testing
If possible, it might be better if you pass the output file path to the program as an argument, so that the file is at some known location:
import sys
from pathlib import Path
p = Path(sys.argv[1])
p.write_text('testing')
print("Testing done")
$ bazel run my_program -- /tmp/output.txt
INFO: Analyzed target //:my_program (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:my_program up-to-date:
bazel-bin/my_program
INFO: Elapsed time: 0.040s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Testing done
$ cat /tmp/output.txt
testing
Alternatively, if you run the program directly, then the file will be in the current working directory:
$ bazel build my_program
INFO: Analyzed target //:my_program (18 packages loaded, 90 targets configured).
INFO: Found 1 target...
Target //:my_program up-to-date:
bazel-bin/my_program
INFO: Elapsed time: 0.219s, Critical Path: 0.01s
INFO: 5 processes: 5 internal.
INFO: Build completed successfully, 5 total actions
$ bazel-bin/my_program
current working directory: /home/ahumesky/test
Testing done
$ cat my_file.txt
testing