python write binary file code example

Example 1: write binary file in python

file = open("sample.bin", "wb")
COPYfile.write(b"This binary string will be written to sample.bin")
COPYfile.close()

Example 2: python write to file

with open(filename,"w") as f:
  f.write('Hello World')

Example 3: write file java

@Test
public void givenUsingJava7_whenWritingToFile_thenCorrect() 
  throws IOException {
    String str = "Hello";
 
    Path path = Paths.get(fileName);
    byte[] strToBytes = str.getBytes();
 
    Files.write(path, strToBytes);
 
    String read = Files.readAllLines(path).get(0);
    assertEquals(str, read);
}