How to generate OSM file from other data

I want to add new data to the existing osm file or create a completely new osm file through my new own data

There are many libraries that parse osm or pbf file, but I couldn’t find one that writes them.

If I want to write an osm file, is there only a way to add data directly according to the xml format?

I would appreciate it if you could tell me how to create a new osm file

As has already been pointed out your workflow is a bit unclear.

But in general if you want to create or edit existing OSM data and don’t want to do that programmatically, you use an “editor” Editors - OpenStreetMap Wiki. At least JOSM and Vespucci can read both PBF and XML format OSM data and write to XML format files.

You could create an OsmChange file that holds your new data, and then use the Osmium Tool to apply these changes to an existing XML (or PBF) file.

I just created the .xml manually. The resulting .osm file can then be merged as noted above (or in my case appended).

I’ve no idea if this actually answers the question since I’m not sure if (a) you can create a .osm (xmm) file but don’t know how to create a .pbf file or (b) you are looking for a library to create a .osm or a .pbf file.

In my case the resulting file is just a .osm file not a .osc one, but either approach works.

PyOsmium can read and write OSM files and allows you to add your own data.

If you want to an easy to create text-based format, you can use OPL and use Osmium to convert it to the more usual OSM formats.