停止Python循环(列表)

huangapple go评论102阅读模式
英文:

Stop python loop (List)

问题

我是Python新手。我有一个循环:

  1. # todaystr给出系统日期的字符串,例如202307
  2. if not os.path.exists(os.path.join(folder, todaystr)):
  3. os.makedirs(os.path.join(folder, todaystr))
  4. for f in os.listdir(folder):
  5. if not (f.startswith(todaystr) and f.endswith(".hl7")):
  6. break
  7. for g in os.listdir(folder)[:4]:
  8. if g.endswith('.hl7') and g.startswith(todaystr):
  9. shutil.move(os.path.join(folder, g), destination)

数据列表:

  1. '202307',
  2. '20230713-0955.hl7',
  3. '20230713-0955.hl7',
  4. '20230713-0955.hl7',
  5. '20230713-0955.hl7',
  6. '20230713-0955.hl7',
  7. '202307XY'

循环应该在列表不再包含以"202307"开头且以"hl7"结尾的数据时停止。问题是有一个名为"202307"的文件夹,所以它立即停止。

谢谢
对不起我的英语不好。

我确定我的if子句内部有问题。

英文:

Im new to python. I have a loop

  1. #todaystr gives string of sysdate e.g. 202307
  2. if not os.path.exists(os.path.join(folder, todaystr )):
  3. os.makedirs(os.path.join(folder, todaystr ))
  4. for f in os.listdir(folder):
  5. if not ( f.startswith(todaystr) and f.endswith("hl7")) :
  6. break
  7. for g in os.listdir(folder)[:4] :
  8. if g.endswith('.hl7') and g.startswith (todaystr) :
  9. shutil.move(os.path.join(folder, g),destination)

list data:

  1. '202307',
  2. '20230713-0955.hl7',
  3. '20230713-0955.hl7',
  4. '20230713-0955.hl7',
  5. '20230713-0955.hl7',
  6. '20230713-0955.hl7',
  7. '202307XY'

The loop should stop as soon as the list contains no data starting with "202307" and ending with "hl7". The Problem is that there is a folder "202307" so he stops instantly.

Thank you
Sorry for my bad english.

Im sure i have a Problem inside my if clause

答案1

得分: 1

看起来你想要类似这样的东西:

  1. import os
  2. folder = "/path/to/folder"
  3. for f in os.listdir(folder):
  4. print(f)
  5. if not (f.startswith("202307") and f.endswith("hl7")):
  6. break
英文:

It sounds like you want something like this:

  1. import os
  2. folder = "/path/to/folder"
  3. for f in os.listdir(folder):
  4. print(f)
  5. if not (f.startswith("202307") and f.endswith("hl7")):
  6. break

答案2

得分: 0

你可以使用glob函数来获取与模式匹配的所有文件,然后确定是否存在一个值。

它可以从glob模块导入,或者使用pathlib.Path类的glob方法。

  1. import os
  2. folder = "/path/to/folder"
  3. print(glob.glob("202307*hl7", root_dir=folder))
  4. """
  5. import pathlib
  6. print(list(pathlib.Path(folder).glob("202307*hl7")))
  7. """
英文:

You can use the glob function to get all the files that match the pattern, and then determine whether there is a value.

It can be imported from the glob module, or using the glob method of the pathlib.Path class.

  1. import os
  2. folder = "/path/to/folder"
  3. print(glob.glob("202307*hl7", root_dir=folder))
  4. """
  5. import pathlib
  6. print(list(pathlib.Path(folder).glob("202307*hl7")))
  7. """

huangapple
  • 本文由 发表于 2023年7月17日 15:23:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定