- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
import shutil
import os
filteredCleaned =
'/Volumes/Extreme_SSD/Raymond_Lab/Day_4_Rotarod_Videos_Rotated_if_Necessary_copy/filtered_cleaned_WT_cleaned_YAC128'
if os.path.exists(filteredCleaned):
shutil.rmtree(filteredCleaned)
^ 显示隐藏文件(按 shift+command+。)和其他目录显示隐藏文件(如果存在)。
runfile('/Users/ksb7640/Documents/UBC_Academic/Raymond_Lab/448/rotarod/svm_all/data_filter.py', wdir='/Users/ksb7640/Documents/UBC_Academic/Raymond_Lab/448/rotarod/svm_all')
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3417, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-b9676f9bf96c>", line 1, in <module>
runfile('/Users/ksb7640/Documents/UBC_Academic/Raymond_Lab/448/rotarod/svm_all/data_filter.py', wdir='/Users/ksb7640/Documents/UBC_Academic/Raymond_Lab/448/rotarod/svm_all')
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/Users/ksb7640/Documents/UBC_Academic/Raymond_Lab/448/rotarod/svm_all/data_filter.py", line 21, in <module>
shutil.rmtree(filteredCleaned)
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 715, in rmtree
_rmtree_safe_fd(fd, path, onerror)
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 672, in _rmtree_safe_fd
onerror(os.unlink, fullname, sys.exc_info())
File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 670, in _rmtree_safe_fd
os.unlink(entry.name, dir_fd=topfd)
FileNotFoundError: [Errno 2] No such file or directory: '._filtered_combined_Experiment2-190630_Day4_145m2_rotarod2_Cam2onRotarodDeepCut_resnet50_rotarod3Jul17shuffle1_1030000.csv'
'._filtered_combined_Experiment2-190630_Day4_145m2_rotarod2_Cam2onRotarodDeepCut_resnet50_rotarod3Jul17shuffle1_1030000.csv'
去哪儿了?从哪里来?
我确实使用了 df.to_csv(csv, index=False)
创建filtered_combined_Experiment2-190630_Day4_145m2_rotarod2_Cam2onRotarodDeepCut_resnet50_rotarod3Jul17shuffle1_1030000.csv
在filtered_cleaned_WT_cleaned_YAC128
但我从未使用过chmod
隐藏它们。
此外,正如您在屏幕截图中所见,甚至没有隐藏文件。错误是什么?
数据过滤器.py
import shutil
from export_df_to_csv import export_df_to_csv
from extract_parent_current import extract_parent_current
from import_df import *
prefix = 'filtered_'
bound = 0.9
# make a directory for output files
cleanedWT = '/Volumes/Extreme_SSD/Raymond_Lab/Day_4_Rotarod_Videos_Rotated_if_Necessary_copy/cleaned_WT'
cleanedYAC128 = '/Volumes/Extreme_SSD/Raymond_Lab/Day_4_Rotarod_Videos_Rotated_if_Necessary_copy/cleaned_YAC128'
cleanedWTParentDir, WTdir = extract_parent_current(cleanedWT)
cleanedYAC128ParentDir, YAC128dir = extract_parent_current(cleanedYAC128)
filteredCleaned = os.path.join(cleanedYAC128ParentDir, prefix + WTdir + '_' + YAC128dir)
if os.path.exists(filteredCleaned):
shutil.rmtree(filteredCleaned)
os.mkdir(filteredCleaned)
csvs_labels_arr = import_csvs(cleanedWT, cleanedYAC128)
paths_dfs_labels_arr = csvs_to_paths_dfs_labels_arr(csvs_labels_arr)
for path_df_label in paths_dfs_labels_arr:
_, dfFileName = extract_parent_current(path_df_label[0])
df = path_df_label[1]
# filter out rows below bound
df = df[
(df['Rightpaw likelihood'] > bound) & (df['Leftpaw likelihood'] > bound) & (df['Tail likelihood'] > bound)]
export_df_to_csv(df, os.path.join(filteredCleaned, prefix + dfFileName))
export_df_to_csv.py
input_file = '/Volumes/Extreme SSD/Raymond Lab/Day_4_Rotarod_Videos_Rotated_if_Necessary copy/cleaned_WT/cleaned_Experiment2-190630_Day4_145m1_rotarod3_Cam2onRotarodDeepCut_resnet50_rotarod3Jul17shuffle1_1030000.csv'
output_file = '/Volumes/Extreme SSD/Raymond Lab/Day_4_Rotarod_Videos_Rotated_if_Necessary copy/cleaned_WT/cleaned_Experiment2-190630_Day4_145m1_rotarod3_Cam2onRotarodDeepCut_resnet50_rotarod3Jul17shuffle1_1030000.csv'
def export_df_to_csv(df, csv):
df.to_csv(csv, index=False)
extract_parent_current.py
import os
def extract_parent_current(dir):
if str.endswith(dir, '/'):
dir = dir[:-1]
return os.path.split(dir)
导入_df.py
import os
import random
from copy import deepcopy
import pandas as pd
def import_csvs(WT_file_path, YAC_file_path):
csv_paths_arr = []
for root, dirs, files in os.walk(WT_file_path, topdown=False):
for file in files:
if not file.startswith('.'):
csv_paths_arr.append([os.path.join(root, file), 0])
for root, dirs, files in os.walk(YAC_file_path, topdown=False):
for file in files:
if not file.startswith('.'):
csv_paths_arr.append([os.path.join(root, file), 1])
return csv_paths_arr
def csvs_to_paths_dfs_labels_arr(csvpaths_labels_arr):
paths_dfs_labels_arr = deepcopy(csvpaths_labels_arr)
for i, csvpath_label_arr in enumerate(csvpaths_labels_arr):
(paths_dfs_labels_arr[i])[0] = pd.read_csv(csvpath_label_arr[0], encoding='unicode_escape')
paths_dfs_labels_arr[i].insert(0, csvpath_label_arr[0])
return paths_dfs_labels_arr
def import_df(WT_file_path, YAC_file_path):
csv_paths_arr = import_csvs(WT_file_path, YAC_file_path)
dfs_labels = csvs_to_paths_dfs_labels_arr(csv_paths_arr)
random.shuffle(dfs_labels)
return dfs_labels
最佳答案
我已经为链接问题 Delete directory and all symlinks recursively 提供了这个答案, 但决定在这里重现它。在那里,它陷入了为潜在问题提供解决方案而不是为实际问题提供答案的谬误,而它在这里直接回答了 ._filtered_combined_Experiment2-190630_Day4_145m2_rotarod2_Cam2onRotarodDeepCut_resnet50_rotarod3Jul17shuffle1_1030000.csv
的来源。
/Volumes/Extreme_SSD/
路径表明您使用的是 Mac OSX,并且您的目录至少部分位于非 Mac 文件系统(即不是 HFS+)上。在这些设备上,Mac 文件系统驱动程序会自动创建前缀为 ._
的二进制伴随文件,以记录所谓的扩展属性(在 https://apple.stackexchange.com/questions/14980/why-are-dot-underscore-files-created-and-how-can-i-avoid-them 中解释,但也在下面说明),在普通操作,例如使用默认 OSX 查看器查看文件。
rmtree
在不支持 os.scandir
中的文件描述符的系统上(例如 Mac OSX)现在会不安全地创建条目列表,然后将它们一一取消链接(创建一个已知的竞争条件:https://github.com/python/cpython/blob/908fd691f96403a3c30d85c17dd74ed1f26a60fd/Lib/shutil.py#L592-L621)。不幸的是,两种不同的行为每次都使这个条件为真:
test.txt
) 时,元文件 (._test.txt
) 会同时删除。因此,扩展属性文件将在轮到它时丢失并抛出您遇到的 FileNotFoundError
。
我认为这个错误最好由 cpython#14064 解决, 旨在忽略 rmtree
中的 FileNotFoundError
。
与此同时,您可以使用 onerror
忽略那些元文件的取消链接错误:
def ignore_extended_attributes(func, filename, exc_info):
is_meta_file = os.path.basename(filename).startswith("._")
if not (func is os.unlink and is_meta_file):
raise
shutil.rmtree(path_dir, onerror=ignore_extended_attributes)
为了说明,您可以创建一个小的 ExFAT 磁盘镜像并使用命令将其装载到 /Volumes/Untitled
hdiutil create -size 5m -fs exfat test.dmg
hdiutil attach test.dmg # mounts at /Volumes/Untitled
cd /Volumes/Untitled
mkdir test # create a directory to remove
cd test
touch test.txt
open test.txt # open the test.txt file in the standard editor
只需在标准文本编辑器中打开文件,就会创建一个扩展属性文件 ._test.txt
并在其中记录上次访问时间:
/Volumes/Untitled/test $ ls -a
. .. ._test.txt test.txt
/Volumes/Untitled/test $ xattr test.txt
com.apple.lastuseddate#PS
问题是自动取消链接原始文件也会取消链接伴随文件。
/Volumes/Untitled/test $ rm test.txt
/Volumes/Untitled/test $ ls -a
. ..
关于python - shutil.rmtree : FileNotFoundError: [Errno 2] No such file or directory: '._xxx' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64687047/
shutil.move(src, dest) 方法是否调用 os.system() 方法或 bash 命令“mv”和“cp”? P.S : 我在网上搜索但找不到任何有用的信息。 最佳答案 使用来源卢克
我正在构建一个相对简单的应用程序,它请求目录,检查它们是否正确,然后删除其中一个并使用另一个目录的内容重新创建它。我遇到了这种奇怪的行为,我会尝试解释: 当我打开目标文件夹窗口并且它是空的时,出现
我正在尝试在Python中使用shutil模块。我想将文件夹 ('movee') 从我的 cwd 复制到 'D' 驱动器。代码如下: import shutil shutil.copytree('mo
我有一个很大的图像数据集(大约 3000 个文件)。我的问题很简单,我想将随机选择的图像文件复制到另一个目的地。我使用 random.sample 选择五百张图片并将它们的名称存储在列表中。我现在想将
昨天,根据一位海报的出色建议,我开始使用 shutil.copyfileobj 方法制作文件副本。 我的程序应该制作文件的精确副本,删除最后一个字节并保存新副本。 我昨晚用一些非常小的 ASCII 文
我有一个文件夹,里面有照片,我想将照片从那个文件夹移动到另一个文件夹。文件: IMAG_01.JPG IMAG_02.JPG IMAG_03.JPG IMAG_04.JPG IMAG_05.JPG I
我正在尝试对shutil.copyfileobj()函数进行猴子修补,以便将其默认长度从16*1024更改为更大的值(128*1024)。在内部,其他 Shutil 方法(如 move)调用 copy
我想移动很多文件。这些文件的路径存储在列表中。我想保留整个目录结构,但想将它们移动到不同的文件夹。 例如文件是D:\test\test1\test1.txtD:\test\test1\test2.tx
尝试处理我的第一个脚本的新 Python 开发人员。目标是编写一个应用程序,通过根据文件类型将文件放入子文件夹来清理我的下载文件夹。 这是我的代码: import shutil import os d
我正在尝试编写一个简单的 python 脚本来移动文件,因为它们进入 ubuntu 中的文件夹。 我正在将文件从我的本地机器移动到我的网络 ubuntu 机器。 请参见下面的代码: f
编程的新手,使用 Python 和“艰难地学习 Python”深入研究它。 出于某种原因,我无法使用 shutil.copy 来复制文件。在我的桌面上,我目前有一个文件“test1.txt”和另一个“
https://docs.python.org/3/library/shutil.html#shutil.move说 If the destination is on the current file
根据http://docs.python.org/library/shutil.html使用这些方法不会复制元数据,包括所有权、ACL 和资源分支。使用基本文件读取方法打开文件并将其逐行写回重复文件是
整个晚上都在为此奋斗。三天前还可以工作,现在我再也无法让它工作了。 当我尝试使用这个时 def unzip_file(archiveFile, finalFolder, archiveFolder)
[编辑:请参阅下面的最终代码]我使用下面的代码从源目录中随机选择 5 个文件,然后将它们复制到新目录。它给了我一个 IO 错误,其中显示“不存在这样的文件或目录‘x’”,其中“x”是不带目录路径的文件
我尝试在析构函数中删除创建的目录: shutil.rmtree("C:\\projects\\project_alpha\\tmp") 它不适用于我的 python 脚本,但是当我通过 python
我想使用函数 shutil.copytree 从我的计算机上的本地服务器复制文件夹,我使用的是 macOS,但今天我遇到了问题,python 总是向我显示相同的消息,“[错误 1] 操作不允许”,但是
我正在尝试使用 python 删除目录,但我不想在此过程中递归删除整个目录路径:即 /home/dir/dir/dirtoberemoved 所以我不想删除更高级别的任何内容,只是一个目录及其所有内容
我正在使用 shutil.copy 将一个文件的内容复制到另一个文件。然而,它导致我的原始文件被删除,并出现“文件中没有数据”的错误 我第一次尝试这个 import shutil sh
我有一个代码,用于将所有 jpg 文件从源移动到目标。代码第一次运行良好,它移动了文件,但如果我再次运行它,它会给出一个文件已存在的错误。 Traceback (most recent call la
我是一名优秀的程序员,十分优秀!