【Kaggle】初学者几个冷门的操作总结

一、如何看当前的目录?

在 Linux 中,你可以使用 pwd 命令来查看当前所在的目录。“pwd” 代表 “Print Working Directory”,它会显示出当前工作目录的路径。

要查看当前目录,请按照以下步骤操作:

  • 打开终端,以进入命令行界面。
  • 输入 pwd 命令,然后按下回车。
  • 系统将显示当前目录的绝对路径,例如 /home/user/Documents。这表示你当前位于名为 “Documents” 的文件夹下。

请注意,Linux 中的路径可以是相对路径或绝对路径。相对路径是相对于当前目录的路径,而绝对路径是从根目录开始的完整路径。pwd 命令始终显示绝对路径。

在Kaggle中的操作为:

!pwd

输出结果为:

在这里插入图片描述

二、Kaggle如何切换路径?

在 Kaggle 上,你可以通过以下步骤切换到输入数据的路径:

  1. 在 Kaggle 网站上打开你的 Kernel(或者 Notebook)。
  2. 点击右侧的 “Data” 选项卡,它会显示出可用的输入数据集。
  3. 在 “Data” 选项卡下,你会看到一个名为 kaggle 的目录。输入数据集通常会被挂载到此目录下。
  4. 使用以下命令切换到输入数据的路径:
import os

input_path = "/kaggle/input"
os.chdir(input_path)

这将把当前工作目录更改为输入数据的路径 /kaggle/input。现在你可以通过该路径访问输入数据集中的文件。

请注意,如果你使用的是 Kaggle Notebook,则输入数据集会自动挂载到 /kaggle/input 目录下。但是,如果你使用的是 Kaggle Kernel,则需要将输入数据集手动添加到 Notebook 设置中,并且在切换到输入数据路径之前,你需要等待输入数据集加载完成。

在这里插入图片描述

三、与包安装或设置有关的错误

我在安装包的过程中,用到了这样的命令:

!python -m pip install -e regionclip-main

但是代码报错:

Obtaining file:///kaggle/input/regionclip-main
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [21 lines of output]
      Traceback (most recent call last):
        File "/kaggle/input/regionclip-main/setup.py", line 174, in get_model_zoo_configs
          os.symlink(source_configs_dir, destination)
      OSError: [Errno 30] Read-only file system: '/kaggle/input/regionclip-main/configs' -> '/kaggle/input/regionclip-main/detectron2/model_zoo/configs'
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/kaggle/input/regionclip-main/setup.py", line 202, in <module>
          package_data={"detectron2.model_zoo": get_model_zoo_configs()},
        File "/kaggle/input/regionclip-main/setup.py", line 177, in get_model_zoo_configs
          shutil.copytree(source_configs_dir, destination)
        File "/opt/conda/lib/python3.10/shutil.py", line 559, in copytree
          return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks,
        File "/opt/conda/lib/python3.10/shutil.py", line 457, in _copytree
          os.makedirs(dst, exist_ok=dirs_exist_ok)
        File "/opt/conda/lib/python3.10/os.py", line 225, in makedirs
          mkdir(name, mode)
      OSError: [Errno 30] Read-only file system: '/kaggle/input/regionclip-main/detectron2/model_zoo/configs'
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.

这似乎是一个与包安装或设置有关的错误,而不是路径切换问题。以下是一些可能的解决方法:

  1. 检查输入数据集和依赖项: 确保你正确添加了输入数据集,并且依赖项和所需的文件都在正确的位置。可能有一些文件或配置缺失或被放置在了不正确的位置。
  2. 检查文件系统权限: 错误信息中提到了 “Read-only file system”,这可能意味着你没有足够的权限在该路径下创建或修改文件。在 Kaggle 上,输入数据集通常是只读的,因此你可能无法修改其中的文件。你可以尝试将文件复制到其他目录中,并在该目录下进行操作。
  3. 联系包的作者或提供者: 错误信息中还提到了 setup.py 文件的错误,这可能是包的安装过程中出现了问题。

四、如何把 Kaggle 上的 input 数据转到 output 中?

在 Kaggle 上,你可以使用以下步骤将输入数据转移到输出目录中:

  1. 在 Kaggle 网站上打开你的 Kernel(或者 Notebook)。
  2. 在 Notebook 中,通过以下代码获取输入数据的路径:
import os
import shutil

input_path = "/kaggle/input"
  1. 然后,使用以下代码创建一个输出目录并将输入数据复制到输出目录中:
output_path = "/kaggle/working/output"
os.makedirs(output_path, exist_ok=True)

# 复制输入数据到输出目录
input_files = os.listdir(input_path)
for file in input_files:
    source = os.path.join(input_path, file)
    destination = os.path.join(output_path, file)
    shutil.copyfile(source, destination)

以上代码会在输出目录 /kaggle/working/output 中创建与输入数据相同的文件,并将输入数据复制到输出目录中。你可以根据需要修改输出目录的路径。

请确保你有足够的磁盘空间来存储复制的文件,并注意一些大型文件可能需要较长的时间进行复制。

完成后,你可以在输出目录中访问并处理这些数据。

发现报错:

IsADirectoryError: [Errno 21] Is a directory: '/kaggle/input/regionclip-main'

完整的报错信息为:

---------------------------------------------------------------------------
IsADirectoryError                         Traceback (most recent call last)
Cell In[8], line 13
     11 source = os.path.join(input_path, file)
     12 destination = os.path.join(output_path, file)
---> 13 shutil.copyfile(source, destination)

File /opt/conda/lib/python3.10/shutil.py:254, in copyfile(src, dst, follow_symlinks)
    252     os.symlink(os.readlink(src), dst)
    253 else:
--> 254     with open(src, 'rb') as fsrc:
    255         try:
    256             with open(dst, 'wb') as fdst:
    257                 # macOS

IsADirectoryError: [Errno 21] Is a directory: '/kaggle/input/regionclip-main'

这个错误表明你正在尝试复制一个目录而不是一个文件。shutil.copyfile() 函数用于复制文件,而不是目录。

如果你想将整个输入目录复制到输出目录中,你可以使用 shutil.copytree() 函数来完成。以下是更新后的代码示例:

import os
import shutil

input_path = "/kaggle/input/regionclip-main"
output_path = "/kaggle/working/output"

shutil.copytree(input_path, output_path)

使用 shutil.copytree() 函数,你可以直接复制整个目录,包括其中的文件和子目录。

再次报错:

---------------------------------------------------------------------------
FileExistsError                           Traceback (most recent call last)
Cell In[9], line 7
      4 input_path = "/kaggle/input/regionclip-main"
      5 output_path = "/kaggle/working/output"
----> 7 shutil.copytree(input_path, output_path)

File /opt/conda/lib/python3.10/shutil.py:559, in copytree(src, dst, symlinks, ignore, copy_function, ignore_dangling_symlinks, dirs_exist_ok)
    557 with os.scandir(src) as itr:
    558     entries = list(itr)
--> 559 return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks,
    560                  ignore=ignore, copy_function=copy_function,
    561                  ignore_dangling_symlinks=ignore_dangling_symlinks,
    562                  dirs_exist_ok=dirs_exist_ok)

File /opt/conda/lib/python3.10/shutil.py:457, in _copytree(entries, src, dst, symlinks, ignore, copy_function, ignore_dangling_symlinks, dirs_exist_ok)
    454 else:
    455     ignored_names = set()
--> 457 os.makedirs(dst, exist_ok=dirs_exist_ok)
    458 errors = []
    459 use_srcentry = copy_function is copy2 or copy_function is copy

File /opt/conda/lib/python3.10/os.py:225, in makedirs(name, mode, exist_ok)
    223         return
    224 try:
--> 225     mkdir(name, mode)
    226 except OSError:
    227     # Cannot rely on checking for EEXIST, since the operating system
    228     # could give priority to other errors like EACCES or EROFS
    229     if not exist_ok or not path.isdir(name):

FileExistsError: [Errno 17] File exists: '/kaggle/working/output'

输出目录 ‘/kaggle/working/output’ 已经存在,并且你希望覆盖其中的内容,可以使用 shutil.rmtree() 函数删除现有目录,然后再执行复制操作。请注意,这将删除输出目录中的所有内容,请确保你已经备份了重要的数据。

以下是更新后的代码示例:

import os
import shutil

input_path = "/kaggle/input/regionclip-main"
output_path = "/kaggle/working/output"

# 删除现有的输出目录
if os.path.exists(output_path):
    shutil.rmtree(output_path)

# 复制目录
shutil.copytree(input_path, output_path)

通过在复制之前删除现有目录,你可以避免 FileExistsError。请确保仔细评估是否需要删除现有目录,以及确认没有重要的数据将被删除。

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旅途中的宽~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值