Liệt kê trong thư mục hiện tại
Với
import glob
txtfiles = []
for file in glob.glob["*.txt"]:
txtfiles.append[file]
4 trong mô -đun import glob
txtfiles = []
for file in glob.glob["*.txt"]:
txtfiles.append[file]
5, bạn nhận được các tệp và các thư mục trong DIR hiện tạiimport os
arr = os.listdir[]
Nhìn vào một thư mục
arr = os.listdir['c:\\files']
Với
import glob
txtfiles = []
for file in glob.glob["*.txt"]:
txtfiles.append[file]
6, bạn có thể chỉ định một loại tệp để liệt kê như thế nàyimport glob
txtfiles = []
for file in glob.glob["*.txt"]:
txtfiles.append[file]
hoặc
mylist = [f for f in glob.glob["*.txt"]]
Nhận toàn bộ đường dẫn của các tệp chỉ trong thư mục hiện tại
import os
from os import listdir
from os.path import isfile, join
cwd = os.getcwd[]
onlyfiles = [os.path.join[cwd, f] for f in os.listdir[cwd] if
os.path.isfile[os.path.join[cwd, f]]]
print[onlyfiles]
['G:\\getfilesname\\getfilesname.py', 'G:\\getfilesname\\example.txt']
Lấy tên đường dẫn đầy đủ với
import glob
txtfiles = []
for file in glob.glob["*.txt"]:
txtfiles.append[file]
7Bạn nhận được con đường đầy đủ để đáp lại
import os
files_path = [os.path.abspath[x] for x in os.listdir[]]
print[files_path]
['F:\\documenti\applications.txt', 'F:\\documenti\collections.txt']
Đi bộ: Đi qua các thư mục phụ
Os.Walk trả về gốc, danh sách các thư mục và danh sách các tệp, đó là lý do tại sao tôi giải nén chúng trong r, d, f trong vòng lặp for; Sau đó, nó tìm kiếm các tệp và thư mục khác trong các thư mục của gốc và cứ thế cho đến khi không có thư mục con.
import os
# Getting the current work directory [cwd]
thisdir = os.getcwd[]
# r=root, d=directories, f = files
for r, d, f in os.walk[thisdir]:
for file in f:
if file.endswith[".docx"]:
print[os.path.join[r, file]]
Đi lên trong cây thư mục
# Method 1
x = os.listdir['..']
# Method 2
x= os.listdir['/']
Nhận các tệp của một thư mục con cụ thể với
import glob
txtfiles = []
for file in glob.glob["*.txt"]:
txtfiles.append[file]
8import os
x = os.listdir["./content"]
os.walk ['.'] - thư mục hiện tại
import os
arr = next[os.walk['.']][2]
print[arr]
>>> ['5bs_Turismo1.pdf', '5bs_Turismo1.pptx', 'esperienza.txt']
Tiếp theo [Os.Walk ['.']] và Os.Path.Join ['Dir', 'File']]
arr = os.listdir['c:\\files']
0Tiếp theo ... đi bộ
arr = os.listdir['c:\\files']
1OS.WALK
arr = os.listdir['c:\\files']
2Os.ListDir [] - Chỉ nhận các tệp TXT
arr = os.listdir['c:\\files']
3Sử dụng
import glob
txtfiles = []
for file in glob.glob["*.txt"]:
txtfiles.append[file]
6 để có được đường dẫn đầy đủ của các tệparr = os.listdir['c:\\files']
4Sử dụng
mylist = [f for f in glob.glob["*.txt"]]
0 để tránh các thư mục trong danh sácharr = os.listdir['c:\\files']
5Sử dụng
mylist = [f for f in glob.glob["*.txt"]]
1 từ Python 3.4arr = os.listdir['c:\\files']
6Với
mylist = [f for f in glob.glob["*.txt"]]
2:arr = os.listdir['c:\\files']
7Sử dụng phương thức GLOB trong pathlib.path []
arr = os.listdir['c:\\files']
8Nhận tất cả và chỉ các tệp có os.walk: Chỉ kiểm tra trong phần tử thứ ba được trả về, tức là danh sách các tệp
arr = os.listdir['c:\\files']
9Chỉ nhận các tệp có tiếp theo trong thư mục: Chỉ trả về tệp trong thư mục gốc
import glob
txtfiles = []
for file in glob.glob["*.txt"]:
txtfiles.append[file]
0Chỉ nhận các thư mục tiếp theo và đi bộ trong một thư mục, bởi vì trong phần tử [1] chỉ có các thư mục
import glob
txtfiles = []
for file in glob.glob["*.txt"]:
txtfiles.append[file]
1Nhận tất cả các tên
mylist = [f for f in glob.glob["*.txt"]]
3 với mylist = [f for f in glob.glob["*.txt"]]
4import glob
txtfiles = []
for file in glob.glob["*.txt"]:
txtfiles.append[file]
2mylist = [f for f in glob.glob["*.txt"]]
5 từ Python 3.5 trở lênimport glob
txtfiles = []
for file in glob.glob["*.txt"]:
txtfiles.append[file]
3