python:获取文件路径并在同一目录中创建一个新文件

时间:2017-10-05 15:21:03

标签: python

所以我知道如何获取所需文件的位置,我用

执行此操作
file_name = os.path.splitext(os.path.basename(file_full_name))[0]

但我需要做的是:

  1. 获取我有的文件名
  2. 获取文件的路径,我也知道如何获取
  3. 使用修改后的名称在同一目录中创建一个文件,例如假设我有一个名为" data.csv"的文件。在桌面上。我需要创建一个名为" data - results.csv"到桌面。
  4. 我只是尝试打印新名称,但这是我用这段代码得到的唯一结果:

        myresultcsvfile = os.path.splitext(os.path.basename(file_name))[0] + " - results.csv"
    

    就是这样:

    myfile: ('Book1 - Copy', ' - results.csv')
    
    我显然做错了什么,但我无法弄清楚是什么。而且这只是文件名,我还需要向其添加父文件的完整路径(以便最终结果为" C:\ users [username] \ desktop \ Book1 - copy - results.csv"在这种情况下)

1 个答案:

答案 0 :(得分:1)

试试这个:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js"></script>


<div class="container carousel-container">
  <div class="row">
    <div class="col">
      <div class="carousel slide" id="carousel-0">
        <!-- change this id and match with that in the JS -->
        <!-- Carousel items -->
        <div class="carousel-inner">
          <div class="active carousel-item" data-slide-number="0">
            <img src="http://placehold.it/770x300&text=one">
          </div>
          <div class="carousel-item" data-slide-number="1">
            <img src="http://placehold.it/770x300&text=two">
          </div>
          <!-- Carousel nav -->
          <a class="carousel-control-prev" href="#carousel-0" role="button" data-slide="prev">
            <!-- change this id and match with that in the JS -->
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
          </a>
          <a class="carousel-control-next" href="#carousel-0" role="button" data-slide="next">
            <!-- change this id and match with that in the JS -->
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
          </a>
        </div>
      </div>
    </div>
  </div>
  <hr>
  <div class="row">
    <div class="col carousel-text"></div>
    <div style="display: none;">
      <!-- only needs the inline CSS -->
      <div class="slide-content" data-slide="0">
        <h5>Slide 1</h5>
        <button class="btn btn-outline-dark btn-sm" data-toggle="modal" data-target="#modal1" type="button" style="margin-left: -1px;">press me</button>
        <div class="dropdown">
          <button class="btn btn-outline-dark btn-sm dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Download</button>
          <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
            <a class="dropdown-item" href="./Downloads/avarice/avarice1/avarice-nodof1280.jpg" download>1280x1024</a>
          </div>
        </div>
      </div>
      <div class="slide-content" data-slide="1">
        <!-- change this id and match with that in the JS -->
        <h5>Slide 2</h5>
        <div class="dropdown">
          <button class="btn btn-outline-dark btn-sm dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Downloads</button>
          <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
            <a class="dropdown-item" href="./Downloads/avarice/avarice0/avarice01280.jpg" download>1280x1024</a>
          </div>
        </div>
      </div>
    </div>
  </div>
  <hr>
  <!--/Slider-->
  <div class="row">
    <div class="col">
      <a class="carousel-selector" data-slide="0"><img src="http://placehold.it/170x100&text=one" class="img-thumbnail"></a>
      <!-- change this id and match with that in the JS -->
    </div>
    <div class="col">
      <a class="carousel-selector" data-slide="1"><img src="http://placehold.it/170x100&text=two" class="img-thumbnail"></a>
      <!-- change this id and match with that in the JS -->
    </div>
  </div>
</div>
<div class="container carousel-container">
  <div class="row">
    <div class="col">
      <div class="carousel slide" id="carousel-1">
        <!-- change this id and match with that in the JS -->
        <!-- Carousel items -->
        <div class="carousel-inner">
          <div class="active carousel-item" data-slide-number="0">
            <img src="http://placehold.it/770x300&text=one">
          </div>
          <div class="carousel-item" data-slide-number="1">
            <img src="http://placehold.it/770x300&text=two">
          </div>
          <!-- Carousel nav -->
          <a class="carousel-control-prev" href="#carousel-1" role="button" data-slide="prev">
            <!-- change this id and match with that in the JS -->
            <span class="carousel-control-prev-icon" aria-hidden="true"></span>
          </a>
          <a class="carousel-control-next" href="#carousel-1" role="button" data-slide="next">
            <!-- change this id and match with that in the JS -->
            <span class="carousel-control-next-icon" aria-hidden="true"></span>
          </a>
        </div>
      </div>
    </div>
  </div>
  <hr>
  <div class="row">
    <div class="col carousel-text"></div>
    <!-- change this id and match with that in JS -->
    <div style="display: none;">
      <!-- only needs the inline CSS -->
      <div class="slide-content" data-slide="0">
        <!-- change this id and match with that in the JS -->
        <h5>Dreams of Avarice1</h5>
        <div class="dropdown">
          <button class="btn btn-outline-dark btn-sm dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Download</button>
          <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
            <a class="dropdown-item" href="./Downloads/avarice/avarice1/avarice-nodof1280.jpg" download>1280x1024</a>
          </div>
        </div>
      </div>
      <div class="slide-content" data-slide="1">
        <!-- change this id and match with that in the JS -->
        <h5>Avarice Zero1</h5>
        <div class="dropdown">
          <button class="btn btn-outline-dark btn-sm dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Downloads</button>
          <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
            <a class="dropdown-item" href="./Downloads/avarice/avarice0/avarice01280.jpg" download>1280x1024</a>
          </div>
        </div>
      </div>
    </div>
  </div>
  <hr>
  <!--/Slider-->
  <div class="row">
    <div class="col">
      <a class="carousel-selector" data-slide="0"><img src="http://placehold.it/170x100&text=one" class="img-thumbnail"></a>
      <!-- change this id and match with that in the JS -->
    </div>
    <div class="col">
      <a class="carousel-selector" data-slide="1"><img src="http://placehold.it/170x100&text=two" class="img-thumbnail"></a>
      <!-- change this id and match with that in the JS -->
    </div>
  </div>
</div>

<!-- modal -->

<div class="modal fade" id="modal1" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h3>...</h3>
      </div>
      <div class="modal-body">
        <div class="container-fluid carousel-container">
          <div class="container">
            <div class="row mx-auto">
              <div class="col">
                <div class="carousel slide carousel-fade" id="carousel-...">
                  <div class="carousel-inner">
                    <div class="active carousel-item" data-slide-number="0">
                      <img data-src="./Downloads/hoohum" src="./loader-carousel.gif" class="lazy img-thumbnail" width="450" height="250"></div>
                    <div class="carousel-item" data-slide-number="1">
                      <img data-src="./Downloads/hoohum" src="./loader-carousel.gif" class="lazy img-thumbnail" width="450" height="250"></div>
                  </div>
                </div>
              </div>
            </div>
            <hr>
            <div class="row mx-auto">
              <div class="col carousel-text"></div>
              <div style="display: none;">
                <div class="slide-content" data-slide="0">
                  <h5>0ne</h5>
                  <div class="btn-group">
                    <div class="btn-group">
                      <button type="button" class="btn btn-outline-dark btn-sm dropdown-toggle" data-toggle="dropdown">Desktop</button>
                      <div class="dropdown-menu" role="menu">
                        <a class="dropdown-item" href="./Downloads/something/something" download>1280x1024</a>
                      </div>
                    </div>
                    <div class="btn-group">
                      <button type="button" class="btn btn-outline-dark btn-sm dropdown-toggle" data-toggle="dropdown">Dual</button>
                      <div class="dropdown-menu" role="menu">
                        <a class="dropdown-item" href="./Downloads/something/something" download>2560x1024 (Dual)</a>
                      </div>
                    </div>
                    <div class="btn-group">
                      <button type="button" class="btn btn-outline-dark btn-sm dropdown-toggle" data-toggle="dropdown">Triple</button>
                      <div class="dropdown-menu" role="menu">
                        <a class="dropdown-item" href="./Downloads/something/something" download>3840x1024 (Triple)</a>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="slide-content" data-slide="1">
                  <h5>two</h5>
                  <div class="btn-group">
                    <div class="btn-group">
                      <button type="button" class="btn btn-outline-dark btn-sm dropdown-toggle" data-toggle="dropdown">Desktop</button>
                      <div class="dropdown-menu" role="menu">
                        <a class="dropdown-item" href="./Downloads/other/other" download>1280x1024</a>
                      </div>
                    </div>
                    <div class="btn-group">
                      <button type="button" class="btn btn-outline-dark btn-sm dropdown-toggle" data-toggle="dropdown">Dual</button>
                      <div class="dropdown-menu" role="menu">
                        <a class="dropdown-item" href="./Downloads/other/other" download>2560x1024 (Dual)</a>
                      </div>
                    </div>
                    <div class="btn-group">
                      <button type="button" class="btn btn-outline-dark btn-sm dropdown-toggle" data-toggle="dropdown">Triple</button>
                      <div class="dropdown-menu" role="menu">
                        <a class="dropdown-item" href="./Downloads/other/other" download>3840x1024 (Triple)</a>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <hr>
          <!--/Slider-->
          <div class="row mx-auto">
            <div class="col">
              <a class="carousel-selector" data-slide="0"><img data-src="./Downloads/hoohum" src="./loader-thumb.gif" class="lazy img-thumbnail" width="75" height="42"></a>
            </div>
            <div class="col">
              <a class="carousel-selector" data-slide="1"><img data-src="./Downloads/hoohum" src="./loader-thumb.gif" class="lazy img-thumbnail" width="75" height="42"></a>
            </div>
            <div class="col"></div>
            <div class="col"></div>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-outline-dark btn-sm" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>