“Python iterate Files” Kode Jawaban

Iterasi melalui semua file dalam direktori Python

import os
directory = 'the/directory/you/want/to/use'

for filename in os.listdir(directory):
    if filename.endswith(".txt"):
      #do smth
      continue
    else:
    continue
Bored Coder

Python berulang melalui file di direktori

import os

for filename in os.listdir(directory):
    if filename.endswith(".asm") or filename.endswith(".py"): 
         # print(os.path.join(directory, filename))
        continue
    else:
        continue
Fair Fowl

Python iterate melalui file

import os

directory = os.fsencode(directory_in_str)
    
for file in os.listdir(directory):
     filename = os.fsdecode(file)
     if filename.endswith(".asm") or filename.endswith(".py"): 
         # print(os.path.join(directory, filename))
         continue
     else:
         continue
Generic name

Loop melalui file python

with open('topology_list.txt') as topo_file:
    for line in topo_file:
        print line,  # The comma to suppress the extra new line char
Difficult Dragonfly

Python iterate Files

# import required module
import os
# assign directory
directory = 'files'
 
# iterate over files in
# that directory
for filename in os.listdir(directory):
    f = os.path.join(directory, filename)
    # checking if it is a file
    if os.path.isfile(f):
        print(f)
Easy Elephant

Jawaban yang mirip dengan “Python iterate Files”

Pertanyaan yang mirip dengan “Python iterate Files”

Lebih banyak jawaban terkait untuk “Python iterate Files” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya