When I open a file, I have to specify the directory that it is in. Is there a way to specify using the current directory instead of writing out the path name? I'm using:
source = os.listdir("../mydirectory")
But the program will only work if it is placed in a directory called "mydirectory". I want the program to work in the directory it is in, no matter what the name is.
def copyfiles(servername):
source = os.listdir("../mydirectory") # directory where original configs are located
destination = '//' + servername + r'/c$/remotedir/' # destination server directory
for files in source:
if files.endswith("myfile.config"):
try:
os.makedirs(destination, exist_ok=True)
shutil.copy(files,destination)
except:
this is a pathlib version:
from pathlib import Path
HERE = Path(__file__).parent
source = list((HERE / "../mydirectory").iterdir())
if you prefer os.path:
import os.path
HERE = os.path.dirname(__file__)
source = os.listdir(os.path.join(HERE, "../mydirectory"))
note: this will often be different from the current working directory
os.getcwd() # or '.'
__file__ is the filename of your current python file. HERE is now the path of the directory where your python file lives.
'.' stands for the current directory.
try:
os.listdir('./')
or:
os.listdir(os.getcwd())
Related
Looking for thoughts on how to read all csv files inside a folder in a project.
As an example, the following code is a part of my present working code, where my 'ProjectFolder' is on Desktop, and I am hardcoding the path. Inside the project folder, I have 'csvfolder' where I have all my csv files
However if I move the "ProjectFolder" to a different Hard drive, or other location, my path fails and I have to provide a new path. Is there an smart way to not worry about location of the project folder?
path = r'C:\Users\XXX\Desktop\ProjectFolder\csvFolder' # use your path
all_files = glob.glob(path + "/*.csv")
df_mm = pd.concat((pd.read_csv(f, usecols=["[mm]"]) for f in all_files),
axis = 1, ignore_index = True)
We have dynamic and absolute path concepts, just search on google "absolute vs relative path"; in your case if your python file is in the ProjectFolder you can simply try this:
from os import listdir
from os.path import dirname, realpath, join
def main():
# This is your project directory
current_directory_path = dirname(realpath(__file__))
# This is your csv directory
csv_files_directory_path = join(current_directory_path, "csvFolder")
for each_file_name in listdir(csv_files_directory_path):
if each_file_name.endswith(".csv"):
each_csv_file_full_path = join(csv_files_directory_path, each_file_name)
# Do whatever you want with each_csv_file_full_path
if __name__ == '__main__':
main()
Knowing the name of my folder "folder_name",
how can i easily get in python (script not in the same folder) the absolute path "/home/user/../path/to/../folder_name" ?
import os
directory = os.path.dirname(__file__)
file_path = os.path.join(directory, './filename.extension')
print(file_path)
I would like to use the shutil.move() function to move some files which match a certain pattern to a newly created(inside python script)folder, but it seems that this function only works with existing folders.
For example, I have 'a.txt', 'b.txt', 'c.txt' in folder '/test', and I would like to create a folder '/test/b' in my python script using os.join() and move all .txt files to folder '/test/b'
import os
import shutil
import glob
files = glob.glob('./*.txt') #assume that we in '/test'
for f in files:
shutil.move(f, './b') #assume that './b' already exists
#the above code works as expected, but the following not:
import os
import shutil
import glob
new_dir = 'b'
parent_dir = './'
path = os.path.join(parent_dir, new_dir)
files = glob.glob('./*.txt')
for f in files:
shutil.move(f, path)
#After that, I got only 'b' in '/test', and 'cd b' gives:
#[Errno 20] Not a directory: 'b'
Any suggestion is appreciated!
the problem is that when you create the destination path variable name:
path = os.path.join(parent_dir, new_dir)
the path doesn't exist. So shutil.move works, but not like you're expecting, rather like a standard mv command: it moves each file to the parent directory with the name "b", overwriting each older file, leaving only the last one (very dangerous, because risk of data loss)
Create the directory first if it doesn't exist:
path = os.path.join(parent_dir, new_dir)
if not os.path.exists(path):
os.mkdir(path)
now shutil.move will create files when moving to b because b is a directory.
I am working on a project on path D:/code/project.
project.py
import os
import argparse
def create(path):
if os.path.exists(path):
os.chdir(path)
app = open("app.py", "w+")
app.write("print(\"Hello world!\")")
else:
path_list = path.split("/")
for i in path_list:
try:
os.mkdir(i)
except FileExistsError:
pass
except FileNotFoundError:
print("Invalid path")
exit()
os.chdir(i)
app = open("app.py", "w+")
app.write("print(\"Hello world!\")")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
create_parser = parser.add_subparsers().add_parser("create")
create_parser.add_argument("path", nargs="?", default=".", metavar="path", type=str)
args = parser.parse_args()
create(vars(args)["path"])
Basically, it has a custom command called create which takes path as an argument. When it detects that path already exists, it will create a app.py at the directory specified, and if it does not, it will try and create the path and app.py.
However, when I run
D:/code/project> python project.py create D:/newpath
Instead of creating a new directory newpath under D:, it creates newpath under my current directory (D:/code/project).
How do I change it such that changing directory to a root directory will actually switch correctly?
Your issue is with this line:
path_list = path.split("/")
On windows, that doesn't correctly split the path. It leaves the drive letter 'D:' as a fragment all by itself, but when you try to change directory to that path, it does nothing (assuming the current directory was on D: somewhere). It is the same behavior you get with the cd command on a command prompt. Drive letters in paths are pretty weird, they don't work like normal path prefixes.
To correctly split the path, use os.path.split(path). It will correctly use 'D:/' as the first element of the path, which when you change to it will put you on the root folder of your drive.
If you're using a recent version of Python (3.4+), you could also try using the pathlib module for a better, more object oriented way of manipulating paths:
from pathlib import Path
def create(path_str):
path = Path(path_str)
path.mkdir(parents=True, exist_ok=True) # creates parent folders as needed
with open(path / 'app.py', 'w') as app: # / operator joins path fragments
app.write('print("Hello world!")')
I am doing a school assignment where I have to take input from a user and save it to a text file.
My file structure will be something like:
- Customer register
- Customer ID
- .txt files 1-5
It can be saved in the python folder and I can make the folders like this:
os.makedirs("Customer register/Customer ID")
My question is, how do I set the path the text files are to be stored in, in the directory when I don't know the directory? So that no matter where the program is run it is saved in the "Customer ID" folder I create (but on the computer the program is run on)?
Also, how do I make this work on both windows and mac?
I also want to program to be able to be executed several times, and check if the folder is there and save to the "Customer ID" folder if it already exists. Is there a way to do that?
EDIT:
This is the code I am trying to use:
try:
dirs = os.makedirs("Folder")
path = os.getcwd()
os.chdir(path + "/Folder")
print (os.getcwd())
except:
if os.path.exists:
path = os.getcwd()
unique_filename = str(uuid.uuid4())
customerpath = os.getcwd()
os.chdir(customerpath + "/Folder/" + unique_filename)
I am able to create a folder and change the directory (everything in "try" works as I want).
When this folder is created I want to create a second folder with a random generated folder name (used for saving customer files). I can't get this to work in the same way.
Error:
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\Users\48736\PycharmProjects\tina/Folder/979b9026-b2f6-4526-a17a-3b53384f60c4'
EDIT 2:
try:
os.makedirs("Folder")
path = os.getcwd()
os.chdir(path + "/Folder")
print (os.getcwd())
except:
if os.path.exists:
path = os.getcwd()
os.chdir(os.path.join(path, 'Folder'))
print(os.getcwd())
def userId(folderid):
try:
if not os.path.exists(folderid):
os.makedirs(folderid)
except:
if os.path.exists(folderid):
os.chdir(path + "/Folder/" + folderid)
userId(str(uuid.uuid4()))
print(os.getcwd())
So I can now create a folder, change directory to the folder I have created and create a new folder with a unique filename within that folder.
But I can't change the directory again to the folder with the unique filename.
Any suggestions?
I have tried:
os.chdir(path + "/Folder/" + folderid)
os.chdir(path, 'Folder', folderid)
os.chdir(os.path.join(path, 'Folder', folderid))
But is still just stays in: C:\Users\47896\PycharmProjects\tina\Folder
You can use relative paths in your create directory command, i.e.
os.makedirs("./Customer register/Customer ID")
to create folder in project root (=where the primary caller is located) or
os.makedirs("../Customer register/Customer ID") in parent directory.
You can, of course, traverse the files tree as you need.
For specific options mentioned in your question, please, see makedirs documentation at Python 3 docs
here is solution
import os
import shutil
import uuid
path_on_system = os.getcwd() # directory where you want to save data
path = r'Folder' # your working directory
dir_path = os.path.join(path_on_system, path)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
file_name = str(uuid.uuid4()) # file which you have created
if os.path.exists(file_name) and os.path.exists(dir_path):
shutil.move(file_name,os.path.join(dir_path,file_name))
else:
print(" {} does not exist".format(file_name))