Python script method for generating Caffe Train_list.txt

Source: Internet
Author: User
Below for you to share a Python script to generate Caffe Train_list.txt method, with a good reference value, I hope to help you. Come and see it together.

First give the code:

Import Ospath = "/home/data//" path_exp = Os.path.expanduser (path) classes = [Int (p) for p in Os.listdir (PATH_EXP)]classes. Sort () # nrof_classes How many folders are there in a dataset, which means how many individuals, how many categories nrof_classes = Len (classes) count=0files = open ("Train_list.txt", ' W ' ) filets = open ("Test_list.txt", ' W ') count_u=0for I in Range (nrof_classes): class_name = str (classes[i]) count=count+1 Co    Unt_u=count_u+1 Facedir = Os.path.join (Path_exp, class_name) prefix1 = path+class_name+ "/" If Os.path.isdir (facedir):     Images = Os.listdir (facedir) #print (images[0]) image_paths = [(prefix1+img+ "" +class_name+ "\ n") for IMG in images] #print (Image_paths[0]) if Count < 0.8*nrof_classes:if len (image_paths) >4:test_path=[] fo     R x in range (2): Test_path.append (Image_paths[0]) del Image_paths[0] Filets.writelines (Test_path) Files.writelines (image_paths) #if count==2: # Break #imgae_pathses = [] #防止图像大小为0 #for x in Image_path S: # if Os.path.getsize (x) &GT;0: # imgae_pathses.append (x) #if len (imgae_pathses) ==0: # Os.rmdir (Facedir) files.close () Filets.close () 

A useful usage of the OS module under Python:

0 Renaming: Files and folders are all a command:

Os.rename (Original_dir,new_dir)

1 File operations:

Os.mknod ("test.txt") Create an empty file
fp = open ("Test.txt", W) opens a file directly and creates a file if the file does not exist

About open mode:

W opens in write mode,
A opens in Append mode (starting with EOF and creating a new file if necessary)
R+ Open in read-write mode
w+ Open in read/write mode (see W)
A + opens in read/write mode (see a)
RB opens in binary read mode
WB opens in binary write mode (see W)
AB opens in binary append mode (see a)
Rb+ opens in binary read/write mode (see r+)
Wb+ opens in binary read/write mode (see w+)
Ab+ opens in binary read/write mode (see A +)

Fp.read ([size]) #size为读取的长度, in bytes

Fp.readline ([size]) #读一行, if size is defined, it is possible to return only part of a row

Fp.readlines ([size]) #把文件每一行作为一个list的一个成员 and returns the list. In fact, its internal is through the Loop call ReadLine () to achieve. If you provide a size parameter, size is the total length of the read content, which means that it may be read only to a portion of the file.

Fp.write (str) #把str写到文件中, write () does not add a newline character after Str

Fp.writelines (seq) #把seq的内容全部写到文件中 (multi-line write-once). This function simply writes faithfully and does not add anything behind each line.

Fp.close () #关闭文件. Python will automatically close files after a file is not used, but this feature is not guaranteed and it is best to develop a habit of shutting them down. If a file is closed and then manipulated, it generates VALUEERROR

Fp.flush () #把缓冲区的内容写入硬盘

Fp.fileno () #返回一个长整型的 "file label"

Fp.isatty () #文件是否是一个终端设备文件 (on UNIX systems)

Fp.tell () #返回文件操作标记的当前位置, starting with the origin of the file

Fp.next () #返回下一行 and shifts the file action marker to the next line. When a file is used for a statement such as for ... in file, it is called the next () function to implement the traversal.

Fp.seek (Offset[,whence]) #将文件打操作标记移到offset的位置. This offset is generally calculated relative to the beginning of the file and is generally a positive number. However, if the whence parameter is provided, whence can be calculated from scratch for 0, and 1 for the current position as its origin. 2 means that the end of the file is calculated as the origin. Note that if the file is opened in a or a + mode, the file action tag is automatically returned to the end of the file each time the write operation is made.

Fp.truncate ([size]) #把文件裁成规定的大小, the default is the location that is cropped to the current file action tag. If the size of the file is larger, depending on the system may not change the file, it may be 0 files to the corresponding size, it may be some random content to add.

You can also open a new file directly:

  With open (' Path0.txt ', ' AB ') as F: for    D in arr:      np.savetxt (f,d,fmt= '%5f ')

Path0.txt will be created directly in the directory where the Python script is located

Arr is a numpy array of shape is the height x Width x channel, note that this is written here because NumPy can only hold data at one height x width dimension at a time

2 New, delete folder

NEW: Os.makedirs ()

For example, under Windows new e:\\dir\\subdir\\

Os.makedirs (' e:\\dir\\subdir\\ ') or os.makedirs (' e:/dir/subdir/')

New under Ubuntu is Os.makedirs ('/home/dir/subdir/')

To copy a file:

Shutil.copyfile ("Oldfile", "NewFile") Oldfile and NewFile can only be files
Shutil.copy ("Oldfile", "NewFile") Oldfile can only be a folder, NewFile can be a file, or it can be a destination directory

To copy a folder:

Shutil.copytree ("Olddir", "Newdir") Olddir and Newdir can only be directories, and newdir must not exist

Renaming files (directories)

Os.rename ("Oldname", "newname") files or directories are all using this command

Moving Files (directories)

Shutil.move ("Oldpos", "Newpos")

deleting files

Os.remove ("file")

Delete Directory

Os.rmdir ("dir") can only delete empty directories
Shutil.rmtree ("dir") empty directory, contents of the directory can be deleted

Converting catalogs

Os.chdir ("path") Change path

Get File Size: os.path.getsize (filename)

3 It is important to note that under Ubuntu there may be paths including ~, so you need to expand, expand using:

Path_exp = Os.path.expanduser (NAM)

4 There is also a case where the path is connected, the first level of the directory will need to be connected, the OS will automatically handle the level of the directory between the/, for example, the directory/home and data 1 is connected together:

Os.path.join ('/home ', str (1)),

The following can also be used for the file name, the same usage

5 path exists: Used to determine if the directory exists

A = os.path.exists (dir)

Existence returns TRUE, otherwise false

6 Get a list of directories, which is the list of files or directories under the given directory:

Classes = Os.listdir (PATH_EXP)

Get a list of all the files under Path_exp, including directories and files,

Os.path.dirname (path) #返回文件路径, or the path after the last directory is removed from the given path,

There is also a way to use it in a file:

Os.path.dirname (__file__) for obtaining the path of the file in which it resides

7 Run shell command: Os.system ():

Under Ubuntu: Os.system (' pwd ')

directory path for Python script work: OS.GETCWD ()

Read and SET Environment variables: os.getenv () and os.putenv ()

Gives the line terminator used by the current platform: os.linesep Windows uses ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R '

Indicates the platform you are using: Os.name for Windows, it is ' NT ', and for Linux/unix users, it is ' POSIX '

Create a multilevel directory: Os.makedirs (r "C:\python\test")

Create a single directory: Os.mkdir ("Test")

Get file attributes: Os.stat (file)

Modify file permissions and timestamps: Os.chmod (file)

Terminate current process: Os.exit ()

More comprehensive Os.path usage:

Os.path.abspath (PATH) #返回绝对路径
Os.path.basename (PATH) #返回文件名

Os.path.basename (' e:a\b\c.jpg ')

' C.jpg '

Os.path.commonprefix (list) #返回list (multiple paths), the longest path common to all paths.
Os.path.dirname (PATH) #返回文件路径
Os.path.exists (path) #路径存在则返回True, path corruption returns false
Os.path.lexists #路径存在则返回True, path corruption also returns true
Os.path.expanduser (path) #把path中包含的 "~" and "~user" to the user directory
Os.path.expandvars (path) #根据环境变量的值替换path中包含的 "$name" and "${name}"
Os.path.getatime (Path) #返回最后一次进入此path的时间.
Os.path.getmtime (Path) #返回在此path下最后一次修改的时间.
Os.path.getctime (PATH) #返回path的大小
Os.path.getsize (path) #返回文件大小, returns an error if the file does not exist
Os.path.isabs (PATH) #判断是否为绝对路径
os.path.isfile (path) #判断路径是否为文件
Os.path.isdir (PATH) #判断路径是否为目录
Os.path.islink (PATH) #判断路径是否为链接
Os.path.ismount (Path) #判断路径是否为挂载点 ()
Os.path.join (path1[, path2[, ...]) #把目录和文件名合成一个路径
Os.path.normcase (PATH) #转换path的大小写和斜杠
Os.path.normpath (PATH) #规范path字符串形式
Os.path.realpath (PATH) #返回path的真实路径
Os.path.relpath (path[, start]) #从start开始计算相对路径
Os.path.samefile (path1, path2) #判断目录或文件是否相同
Os.path.sameopenfile (FP1, FP2) #判断fp1和fp2是否指向同一文件
Os.path.samestat (STAT1, STAT2) #判断stat tuple STAT1 and Stat2 point to the same file

Os.path.split (path) #把路径分割成dirname和basename, returns a tuple:
Os.path.split (' e:a\b\c.jpg ')

' e:\\a\\b ', ' c.jpg '
Os.path.splitdrive (path) #一般用在windows下, returns a tuple of drive names and paths
Os.path.splitext (path) #分割路径, returns a tuple of pathname and file name extension

Os.path.split (' e:a\b\c.jpg ')

' E:\\a\\b\\c ', '. jpg '

Os.path.splitunc (PATH) #把路径分割为加载点与文件
Os.path.walk (path, visit, Arg) #遍历path, the visit function is called into each directory, and the visit function must have

3 parameters (ARG, dirname, names), dirname represents the directory name of the current directory, names represents all of the current directory
File name, args is the third parameter of walk

Os.path.supports_unicode_filenames #设置是否支持unicode路径名


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.