forked from aimuch/AITools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabspath2txt.py
52 lines (38 loc) · 1.15 KB
/
abspath2txt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
# Author : Andy Liu
# Last modified: 2020-11-02
# This tool is used to convert image abs path into txt
# input: python abspath2txt.py "/home/andy/Data/img"
# output:
# ./imgPath.txt
import argparse
import os,sys
import pathlib
import random
from os import listdir, getcwd
from os.path import join
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('srcdir', help='file directory', type=str)
args = parser.parse_args()
return args
def makelist(srcdir):
srcdir = os.path.abspath(srcdir)
if srcdir[-1] == "/":
srcdir = srcdir[:-1]
txt = "./imgPath.txt"
txtfile=open(txt,'w+') # 'w+' rewrite, 'a' add
filelist = os.listdir(srcdir)
for file in filelist:
file_name,file_extend=os.path.splitext(file)
txtfile.write(srcdir+"/"+file+'\n')
txtfile.close()
print("Path of text = ",os.path.abspath(txt))
if __name__ == '__main__':
args = parse_args()
srcdir = args.srcdir
if not os.path.exists(srcdir):
print("Error !!! %s is not exists, please check the parameter"%srcdir)
sys.exit(0)
makelist(srcdir)
print("Done!")