Python gets the list of files and content in the directory
The following Python example is a file that enumerates the user directories
Import OS
Import Sys
#info =OS.GETCWD ()
#listfile =os.listdir (OS.GETCWD ())
Info=raw_input ("Enter the directory where you want to enumerate the files: (for example, D:\\temp)")
Listfile=os.listdir (Info)
Filename=open (info+ ' file.txt ', ' W ')
Print ListFile
#out =open (listfile, ' R ')
For line in ListFile: #把目录下的文件都赋值给line这个参数
Print Line #打印出赋值的内容
#filename. Write (filename)
If line[-3:] = = '. Py ' or line[-4:] = = ' txt ':
Print Line
Out=open (line, ' R ') #定义读取line里面的内容, which is to read the contents of each file
For COM in out: #把每个文件的内容 (that is, files in the directory) assigned to COM
Filename.write (line+ ":" +com)
Else
Print (line+ ' + "This file is a directory form")
Filename.close ()
Example of running:
>>> Import OS
>>> Import Sys
>>> info=raw_input ("Please enter the directory where you want to enumerate files:")
Please enter the directory to enumerate the files: C:\Python27\//written file directory name
>>> Listfile=os.listdir (Info)//Find all files in the directory
>>> Filename=open (info+ ' file.txt ', ' W ')
>>> Print ListFile
[' Carddata.txt ', ' DLLs ', ' Doc ', ' etc ', ' file.txt ', ' hhga.txt ', ' include ', ' Lib ', ' Libs ', ' LICENSE.txt ', ' ml.txt ', ' myFile ', ' NEWS.txt ', ' numbers ', ' python.exe ', ' pythonw.exe ', ' pywin32-wininst.log ', ' README.txt ', ' Removepywin32.exe ', ' Sample.csv ', ' samples ', ' Scripts ', ' tcl ', ' temp.py ', ' Test ', ' tmp ', ' Tools ', ' unicode.txt ', ' w9xpopen.exe ', ' \xc0\xfd\ Xd7\xd3 ']
>>> for line in ListFile:
Print Line
If line[-3:] = = '. Py ' or line[-4:] = = ' txt ':
Print Line
Out=open (line, ' R ')
For COM in out:
Filename.write (line+ ":" +com)//write readable file ". txt" contents to file.txt file
Else
Print (line+ ' + "This file is a directory form")
Carddata.txt
Carddata.txt
DLLs
DLLs This file is a directory form
Doc
Doc This file is a directory form
etc
Etc This file is a directory form
File.txt
File.txt
Hhga.txt
Hhga.txt
Include
Include this file is a directory form
Lib
Lib This file is a directory form
Libs
Libs This file is a directory form
LICENSE.txt
LICENSE.txt
Ml.txt
Ml.txt
MyFile
MyFile This file is a directory form
NEWS.txt
NEWS.txt
Numbers
Numbers this file is a directory form
Python.exe
Python.exe This file is a directory form
Pythonw.exe
Pythonw.exe This file is a directory form
Pywin32-wininst.log
Pywin32-wininst.log This file is a directory form
README.txt
README.txt
Removepywin32.exe
Removepywin32.exe This file is a directory form
Sample.csv
Sample.csv This file is a directory form
Samples
Samples the file is a directory form
Scripts
Scripts This file is a directory form
Tcl
Tcl the file is a directory form
temp.py
temp.py
Test
Test this file is a directory form
Tmp
TMP This file is a directory form
Tools
Tools This file is a directory form
Unicode.txt
Unicode.txt
W9xpopen.exe
W9xpopen.exe This file is a directory form
Example
Example the file is a directory form