【问题标题】:Moving processed data into another folder将处理后的数据移动到另一个文件夹
【发布时间】:2017-04-12 15:15:29
【问题描述】:

所以我试图将一些预处理数据移动到正确的文件夹中,因为我的其他代码没有在正确的位置输出它(被问到另一个问题和我正在处理的事情)并且我一直遇到问题。我的代码发布在下面。我不断收到此错误(发布在下面)。我非常接近,但我需要代码来搜索一个目录以获取文件。它似乎在我的 subject_dir 而不是我的 dti_dir 中查找(即使我在代码中指定了这一点)。我想我可能只是错误地为我的 shutil.move 命令输入了路径。也许我需要用 src 加入 abspath?如果是这样,我不确定正确的语法。我对编码还是很陌生,所以如果这是一个微不足道的问题,我深表歉意!

IOError: [Errno 2] No such file or directory:
'/gandg/infinite/imaging_data/individuals/inf0117/1/dtifit_L1.nii.gz'

基本上我想做的是

  1. 在 src 中查找标题中包含“dtifit_”的所有文件
  2. 将所有这些文件移动到 dst

我非常感谢任何建议,并请原谅任何新手错误,因为我对编码和尝试学习还很陌生!

# Import modules

import _utilities as util
import os
from nipype.interfaces import fsl
import shutil

# Environment and shortcuts

subject_dir = os.getcwd()
dti_dir = os.path.abspath( os.path.join(subject_dir, 'dti'))
output_path     = '../dti/dtifit'
output_basename = os.path.abspath(os.path.join(dti_dir, output_path))

# Move Data

src = dti_dir
dst = output_basename
print('src')
print(src)
print('dst')
print(dst)
files = os.listdir(src)

for f in files:
    if (f.startswith("dtifit_")):
        shutil.move(os.path.abspath((f), dst)

【问题讨论】:

    标签: python python-2.7 unix move-semantics shutil


    【解决方案1】:

    我意识到我知道自己问题的答案,因此我将其发布在这里以帮助处于类似情况的其他人。我是对的,我需要将我的 src 与我的 f 一起加入,但我的 () 在我尝试它的前几次被错误地放置,所以如果你遇到错误,请注意这一点。如果这个问题浪费了这里更有经验的编码人员的时间,我深表歉意!

    for f in files:
        if (f.startswith("dtifit_")):
            shutil.move(os.path.abspath(os.path.join(src,f)), dst)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多