Basic Command#

  • Author: Fu Yin

  • Update: July 29, 2022

  • Reading: 30 min


File-IO Subprocess Numpy h5py

File IO#

read file:

If the file.txt is like the following format whose each line has a separate content:

UTC190419001218.sgy
UTC190419001233.sgy
UTC190419001248.sgy
UTC190419001303.sgy

Read it into a list

file = "./file.txt"
with open(file, "r") as f:
    allfile = []
    for readline in f:
        onefile = readline.rstrip('\n')
        allfile.append(onefile)

write file:

fout = open("input.txt", 'w') 
fout.write('# i \t i+0.5 \n')
for i in range(0,10):
  fout.write(str(i)) 
  fout.write('\t')
  fout.write(str(i+0.5)) 
  fout.write('\n')
fout.close()

Numpy#

import numpy as np

a = np.ones(10)

Pathlib#

from pathlib import Path
output_path = "./DAS_DATA"

# check whether exists
Path(output_path).is_dir()

# make file
Path.mkdir(Path(output_path))

# absolute path
abs_path = Path(output_path).joinpath(url.split('/')[-1]).absolute()

# convert to string
abs_filename = str(abs_path)

Subprocess#

import sys
print("this is some stdddout")
print("this is some stdout")
print("this is some stderr", file=sys.stderr)
print(a)
this is some stdddout
this is some stdout
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[1], line 5
      3 print("this is some stdout")
      4 print("this is some stderr", file=sys.stderr)
----> 5 print(a)

NameError: name 'a' is not defined