对文件名各种操作

os.path —- 常见路径操作
python获取当前目录路径和上级路径
提取文件名

  1. #split里面也可以换成一个用wget下载的链接,方便得知文件名进行移动等操作
  2. import os
  3. name = os.path.split('./dodo/soft/python') #就这个
  4. os.system
  5. print(name)
  6. 'python'
  7. #例如
  8. os.system("wget -c -P ./sra_ing/ %s" %(j)) #j是一个下载链接,-P保存到./sra_ing/ 目录下
  9. name = './sra_ing/'+os.path.split(j)[-1] #所以name记得加路径,否则无法移动
  10. os.system("mv %s ./sra/" %(name))

生成一个文件的完整路径
用join函数,前面os.path.abspath(indir)是sample文件所属文件夹的绝对路径,sample就是文件名。
sample = os.path.join(os.path.abspath(indir),sample)

  • 判断是否为数字if a.isdigit():是数字就返回True,即if成立。
  • re.findall返回值为数组而不是字符串,无论找到几个,所以输出会有中括号。''.join(返回值)可以变回字符串
  • 将科学计数型字符串改回科学计数用eval()
  • python调用linux命令并将输出储存在变量里 链接
    1. import subprocess
    2. output =str(subprocess.check_output("tail -n 1 log.txt", shell=True),"utf-8") #输出的本来是bytes,转义成字符串
    3. print(output)
    这样输出的是’1615\n’,我想要的是1615,可以用re.findall+’’.join。
    shell的话可以直接output=tail -n 1 log.txt``,注意不能有空格,且这么输出只有数字 链接

    传参

    例如 python test.py a1 a2 a3,这样就往里面传入了三个参数,python里面将a1,a2,a3复制给变量就能用了。
    1. import os
    2. import time
    3. import sys
    4. sample=sys.argv[1]
    5. cpu=sys.argv[2]
    6. outtime_file=sys.argv[3] #这些就是将前面a1 a2 a3 传进来,第一个就是1而不是0
    7. #下面这个可以用format将变量传到字符串里面,然后system执行起来
    8. code = 'plass assemble --threads {1} {0} {3}/plassout/{2}.fas tmp'.format(sample,cpu,filename,path) #跑plass的code
    9. os.system(code)

    函数计时

    1. def time_function(f, *args):
    2. """
    3. Call a function f with args and return the time (in seconds) that it took to execute.
    4. """
    5. import time
    6. tic = time.time()
    7. f(*args)
    8. toc = time.time()
    9. return toc - tic
    10. # 使用的时候就是传函数名以及函数要用的参数进去就可以
    11. two_loop_time = time_function(classifier.compute_distances_two_loops, X_test)

    超时跳过

    ```python import threading import time import sys import ctypes import inspect

def _async_raise(tid, exctype): tid = ctypes.c_long(tid) if not inspect.isclass(exctype): exctype = type(exctype) res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype)) if res == 0: raise ValueError(“invalid thread id”) elif res != 1: ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) raise SystemError(“PyThreadState_SetAsyncExc failed”) def stop_thread(thread): _async_raise(thread.ident, SystemExit)

def func1(i): ‘’’ things need to do eg i += 1 ‘’’ if name == ‘main‘: for i in range(10): #主函数里循环for,超时跳过该sample进行下一个 stop = False t = threading.Thread(target=func1, args=(i,)) #可传参,不传的话就把arg那块删掉 t.start() t.join(timeout=0.1) #超时时间设置,这里是0.1s,应该是wall-time print(“it’s over”) if stop != True: stop_thread(t)

  1. #print(222)
  2. continue
  3. print(111)
  1. <a name="qvsyC"></a>
  2. # 基础知识
  3. <a name="jHoWt"></a>
  4. ## if __name__ == __mian__
  5. if __name__ == '__main__'的意思是:当.py文件被直接运行时,if __name__ == '__main__'之下的代码块将被运行;当.py文件以模块形式被导入时,if __name__ == '__main__'之下的代码块不被运行。<br />所以就可以既封装好直接调用,又可以import只调用某个函数
  6. <a name="n3Ln7"></a>
  7. ## 遍历文件夹
  8. [Link](https://zhuanlan.zhihu.com/p/98124110) 用os.walk,然后返回出文件名和路径,join起来就得到绝对路径,再对文件进行操作。
  9. ```python
  10. def findAllFile(base):
  11. for root, ds, fs in os.walk(base):
  12. for f in fs:
  13. fullname = os.path.join(root, f)
  14. print(fullname)
  15. if __name__ == '__main__':
  16. findAllFile('./base')

将linux输出转作python变量

Link 用commands.getoutput

  1. import commands
  2. cmd_getlines = 'cat {0} | wc -l'.format(fullname)
  3. lines = commands.getoutput(cmd_getlines)