How can I do the following in Python?
for file in os.listdir(directory):
do sth
output file to the folder
if filenumber in the folder >=100:
break
I think the first line for file in folder
can find the newly created files.
Instead it just loop through the files that existed when the sript starts.
Any idea to make it work? Thanks.
os.listdir
gives you a list of files and subdirectories in the given directory at the moment it is called. Any changes you make to the directory are not seen. You could keep rereading the directory
while True:
files = os.listdir(directory)
if len(files) >= 100:
break
new_file = do_a_thing()
open(new_file).write("foo")
Or just do the math once at the start
files = os.listdir(directory)
delta = 100 - len(files)
if delta > 0:
for file in files[:delta]:
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.