Check and move directories in Python

Python operations and directories

When reading a data file when operating Python, The directory in which the file is stored must be the current directory. In this memo, the directory where the data is already stored is The method of setting as the current directory is described.

OS module import

To work with directories, you need to import the os module. Enter the following sentence.

import os

Below, first check where the current directory is set, and then set the directory containing the data you want to read to the current directory.

Check the current directory

os.getcwd() 

The result is below.

>>> os.getcwd()
'C:\\Users\\ozspade\\AppData\\Local\\Continuum\\Anaconda3\\Scripts'

I don't put the analysis data in such a place. I put the data below.

C:\\Users\\ozspade\\Documents\\PythonScripts

Set this to the current directory.

Move directory

Enter as follows.

os.chdir("C:\\Users\\ozspade\\Documents\\PythonScripts")

This completes the setting, and check the current directory again.

>>> os.getcwd()
'C:\\Users\\ozspade\\Documents\\PythonScripts'

I was able to safely specify the directory where the analysis data is located. I'm happy.

reference

KURA IT LAB

Recommended Posts

Check and move directories in Python
Check and receive Serial port in Python (Port check)
Find and check inverse matrix in Python
Stack and Queue in Python
Unittest and CI in Python
Write tests in Python to profile and check coverage
Recursively search for files and directories in Python and output
Check for memory leaks in Python
[Python] Check the current directory, move the directory
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Check for external commands in python
Difference between == and is in python
View photos in Python and html
Sorting algorithm and implementation in Python
Manipulate files and folders in Python
About dtypes in Python and Cython
Assignments and changes in Python objects
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Function synthesis and application in Python
Export and output files in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
Overlapping regular expressions in Python and Java
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Check the behavior of destructor in Python
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
Line graphs and scale lines in python
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Search and play YouTube videos in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
Write O_SYNC file in C and Python
How to check opencv version in python
Dealing with "years and months" in Python
Read and write JSON files in Python
Easily graph data in shell and Python
Private methods and fields in python [encryption]
Linear Independence and Basis: Linear Algebra in Python <6>
Call sudo in Python and autofill password
Put Tkinter in Macbook and check operation
Differences in multithreading between Python and Jython
Module import and exception handling in python
How to use is and == in Python
SublimeText2 and SublimeLinter --Syntax check for Python3--
Project Euler # 1 "Multiples of 3 and 5" in Python
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Python nan check