在路径中创建一个带有变量的文件夹(Python 3.6)

时间:2017-07-19 14:58:15

标签: python python-3.x directory

我希望创建一个函数,在运行它时会在当前工作目录中创建一个带有两个子文件夹(输入和输出)的新时间戳文件夹。我的Python技能不存在(之前从未使用过)。我可以制作带时间戳的文件夹,但是如何在不必硬编码路径名的情况下最好地创建子文件夹?

from os import mkdir
from datetime import datetime

today = datetime.now

new_run = 'H:\model\{}' .format(today().strftime('%d%m%Y'))
mkdir(new_run)

inputfold = 'H:\model\\19072017\{}' .format('input')
outputfold = 'H:\model\\19072017\{}' .format('output')
mkdir(inputfold)
mkdir(outputfold)

我没有inputfold = 'H:\model\\19072017\{}' .format('input')而是希望使用变量名来代替时间戳,以便在运行模型的每个新的一天时,您不必手动更改路径inputfold = 'H:\FSM IV\\***today***\{}' .format('input')。我使用的是Python 3.6。

2 个答案:

答案 0 :(得分:1)

 package com.example.abdelmagied.myapplication;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;

/**
 * Created by AbdELMagied on 7/19/2017.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>{
    private ArrayList<items> myitems;
    public LayoutInflater inflater;
    public RecyclerAdapter(Context context, ArrayList<items> myitems) {
        this.myitems = myitems;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      View v = inflater.inflate(R.layout.recyclerviewrow , parent , false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerAdapter.MyViewHolder holder, int position) {
           holder.name.setText(myitems.get(position).name);
        holder.price.setText(myitems.get(position).price);
    }

    @Override
    public int getItemCount() {
        return myitems.size();
    }

    public static class MyViewHolder extends  RecyclerView.ViewHolder{
         TextView name;
         TextView price;
        public MyViewHolder(View itemView) {
           super(itemView);
            name = (TextView) itemView.findViewById(R.id.recyclerviewname);
            price = (TextView)itemView.findViewById(R.id.recyclerviewprice);
        }
    }
}

答案 1 :(得分:0)

我不是使用字符串格式,而是使用内置功能来做这件事的巨大粉丝。您可以在标准库中使用os.pathpathlib(从Python 3.4开始pathlib)。

使用os.path

import os.path
from os import mkdir
from datetime import datetime

# No need to wait to call the function... do it now!
today = datetime.now()

# Make the time stamp.  Here I am demonstrating you can
# Do the formatting with the .format operator directly.
time_stamp = '{:%d%m%Y}'.format(today)
# Alternatively
#time_stamp = format('{:%d%m%Y}', today)

# Create the path names.
# Variables can be used directly! No need to worry about
# using raw strings or escaping the '\' character!
new_run = os.path.join('H:', 'model', time_stamp)
inputfold = os.path.join(new_run, 'input')
outputfold = os.path.join(new_run, 'output')
# Alternatively to the above
#inputfold = os.path.join('H:', 'model', time_stamp, 'input')
#outputfold = os.path.join('H:', 'model', time_stamp, 'output')

# Create!  os.makedirs is also good.
mkdir(new_run)
mkdir(inputfold)
mkdir(outputfold)

以下是使用pathlib

执行此操作的方法
import pathlib
from os import mkdir
from datetime import datetime

# No need to wait to call the function... do it now!
today = datetime.now()

# Make the time stamp.  Here I am demonstrating you can
# Do the formatting with the .format operator directly.
time_stamp = '{:%d%m%Y}'.format(today)

# Create the path names.
# Variables can be used directly! No need to worry about
# using raw strings or escaping the '\' character!
new_run = pathlib.Path('H:', 'model', time_stamp)
# pathlib lets you use the / operator to build paths. Super cool!
inputfold = new_run / 'input'
outputfold = new_run / 'output'

# Create the folders, ensuring all parents are also created.
inputfold.mkdir(parents=True)
outputfold.mkdir(parents=True)