我试图只获取文件夹中的文件,不包括任何其他目录。但是下面的脚本正在将所有文件和文件夹移动到另一个目录。
while True:
# Go through each of the directories
for mylist in dirlist:
check_dir = mylist[0]
callback = mylist[1]
# get the list of files in the directory
filelist = os.listdir(check_dir)
for this_file in filelist:
if ((this_file == ".") or (this_file == "..") or (this_file == "donecsvfiles") or (this_file == "doneringofiles")):
print "Skipping self and parent"
continue
full_filename = "%s/%s"%(check_dir, this_file)
答案 0 :(得分:1)
在 os.path 中,有一些有用的isdir()和isfile()函数:
public KeyboardService() {
keyboardPopup = KeyBoardPopupBuilder.create().initLocale(Locale.GERMAN).build();
keyboardPopup.setAutoHide(true);
keyboardPopup.setConsumeAutoHidingEvents(false);
keyboardPopup.getKeyBoard().setScale(2.5);
keyboardPopup.getKeyBoard().setLayer(DefaultLayer.DEFAULT);
keyboardPopup.getKeyBoard().setOnKeyboardCloseButton((e) -> {
keyboardPopup.hide();
});
// listen to width changes and center
keyboardPopup.widthProperty().addListener((obs, ov, nv) -> {
keyboardPosX = (screenWidth - nv.doubleValue()) / 2d;
keyboardPopup.getScene().getWindow().setX(keyboardPosX);
});
}
public void showKeyboard(Node node) {
keyboardPosX = (screenWidth - keyboardPopup.getWidth())/2;
keyboardPosY = screenHeight;
keyboardPopup.show(node, keyboardPosX, keyboardPosY);
}
此外,您可以使用os.walk()或os.scandir()自动将两者分开:
>>> import os
>>> import os
>>> os.path.isfile('demo.py')
True
>>> os.path.isdir('demo.py')
False
>>> os.path.isfile('__pycache__')
False
>>> os.path.isdir('__pycache__')
答案 1 :(得分:0)
您可以使用pd.to_timedelta(df['ride_duration'].astype(str).replace(r"\s+","",regex=True))
中的isfile
来检查路径是否为文件。此代码为您提供文件夹中的文件列表:
os.path
答案 2 :(得分:0)
Python 3.5+提供了一个生成器,它通常比列表推导和循环更有效。您可以通过for i in arr
进行遍历,通过next(arr)
进行迭代,也可以通过list(arr)
获取完整列表。
import os
arr = os.scandir('H:\public\python') # returns a generator
答案 3 :(得分:0)
几乎直接来自docs:
import os
with os.scandir('.') as it:
for entry in it:
if entry.is_file():
print(entry)