os.path.exists is not working as expected in python - python

I am trying to create a directory in the home path and re-check if the directory exists in the home path before re-creating using os.path.exists(), but its not working as expected.
if os.access("./", os.W_OK) is not True:
print("Folder not writable")
dir_name_tmp = subprocess.Popen('pwd', stdout=subprocess.PIPE, shell=True)
dir_name_tmp = dir_name_tmp.stdout.read()
dir_name = dir_name_tmp.split('/')[-1]
dir_name = dir_name.rstrip()
os.system('ls ~/')
print "%s"%dir_name
if not os.path.exists("~/%s"%(dir_name)):
print "Going to create a new folder %s in home path\n"%(dir_name)
os.system('mkdir ~/%s'%(dir_name))
else:
print "Folder %s Already Exists\n"%(dir_name)
os.system('rm -rf ~/%s & mkdir ~/%s'%(dir_name, dir_name))
else :
print("Folder writable")
Output for the first time:
Folder not writable
Desktop Downloads Perforce bkp doc project
hello.list
Going to create a new folder hello.list in home path
Output for the 2nd time:
Folder not writable
Desktop Downloads Perforce bkp doc hello.list project
hello.list
Going to create a new folder hello.list in home path
mkdir: cannot create directory `/home/desperado/hello.list': File exists
Its not going into the else loop though the directory is existing. Am I missing something ? Share in you inputs !
Updated Working Code With Suggestions Provided: Using $HOME directory and os.path.expandusr
if os.access("./", os.W_OK) is not True:
log.debug("Folder Is Not writable")
dir_name_tmp = subprocess.Popen('pwd', stdout=subprocess.PIPE, shell=True)
dir_name_tmp = dir_name_tmp.stdout.read()
dir_name = dir_name_tmp.split('/')[-1]
dir_name = dir_name.rstrip()
log.debug("dir_name is %s"%dir_name)
dir_name_path = (os.path.expanduser('~/%s'%(dir_name))).rstrip()
log.debug("dir_name_path is %s"%(dir_name_path))
# if not os.path.exists('~/%s'%(dir_name)):
if not os.path.exists('%s'%(dir_name_path)):
log.debug("Going to create a new folder %s in home path\n"%(dir_name))
os.system('mkdir $HOME/%s'%(dir_name))
else:
log.debug("Folder %s Already Exists\n"%(dir_name))
os.system('rm -rf %s'%(dir_name_path))
os.system('mkdir $HOME/%s'%(dir_name))
else :
log.debug("Folder Is Writable")

The tilde symbol ~ representing the home directory is a shell convention. It is expanded by the shell in os.system, but it is understood literally in Python.
So you create <HOME>/<DIR>, but test for ~/<DIR>.

As mentioned by VPfB, the tilde symbol is understood literally by Python. To fix this, you need to get your actual home directory.
Now, on different platforms, there are different paths for the home directory.
To get the home directory, os.path.expanduser will be useful.
>>> import os
>>> os.path.expanduser("~")
'/Users/ashish'

Related

Create directory if does NOT exist: Python [duplicate]

I want to check if a folder by the name "Output Folder" exists at the path
D:\LaptopData\ISIS project\test\d0_63_b4_01_18_ba\00_17_41_41_00_0e
if the folder by the name "Output Folder" does not exist then create that folder there.
can anyone please help with providing a solution for this?
The best way would be to use os.makedirs like,
os.makedirs(name, mode=0o777, exist_ok=False)
Recursive directory creation function. Like mkdir(), but makes a intermediate-level directories needed to contain the leaf directory.
The mode parameter is passed to mkdir() for creating the leaf directory; see the mkdir() description for how it is interpreted. To set the file permission bits of any newly-created parent directories you can set the umask before invoking makedirs(). The file permission bits of existing parent directories are not changed.
>>> import os
>>> os.makedirs(path, exist_ok=True)
# which will not raise an error if the `path` already exists and it
# will recursively create the paths, if the preceding path doesn't exist
or if you are on python3, using pathlib like,
Path.mkdir(mode=0o777, parents=False, exist_ok=False)
Create a new directory at this given path. If mode is given, it is combined with the process’ umask value to determine the file mode and access flags. If the path already exists, FileExistsError is raised.
If parents is true, any missing parents of this path are created as needed; they are created with the default permissions without taking mode into account (mimicking the POSIX mkdir -p command).
If parents is false (the default), a missing parent raises FileNotFoundError. > If exist_ok is false (the default), FileExistsError is raised if the target directory already exists.
If exist_ok is true, FileExistsError exceptions will be ignored (same behavior as the POSIX mkdir -p command), but only if the last
path component is not an existing non-directory file.
Changed in version 3.5: The exist_ok parameter was added.
>>> import pathlib
>>> path = pathlib.Path(somepath)
>>> path.mkdir(parents=True, exist_ok=True)
import os
import os.path
folder = "abc"
os.chdir(".")
print("current dir is: %s" % (os.getcwd()))
if os.path.isdir(folder):
print("Exists")
else:
print("Doesn't exists")
os.mkdir(folder)
I hope this helps
pathlib application where csv files need to be created inside a csv folder under parent directory, from a xlsx file with full path (e.g., taken with Path Copy Copy) provided.
If exist_ok is true, FileExistsError exceptions will be ignored, if directory is already created.
from pathlib import Path
wrkfl = 'C:/xlsx/my.xlsx' # path get from Path Copy Copy context menu
xls_file = Path(wrkfl)
(xls_file.parent / 'csv').mkdir(parents=True, exist_ok=True)
Search for folder whether it exists or not, it will return true or false: os.path.exists('<folder-path>')
Create a new folder: os.mkdir('<folder-path>')
Note: import os will be required to import the module.
Hope you can write the logic using above two functions as per your requirement.
import os
def folder_creat(name, directory):
os.chdir(directory)
fileli = os.listdir()
if name in fileli:
print(f'Folder "{name}" exist!')
else:
os.mkdir(name)
print(f'Folder "{name}" succesfully created!')
return
folder_creat('Output Folder', r'D:\LaptopData\ISIS project\test\d0_63_b4_01_18_ba\00_17_41_41_00_0e')
This piece of code does the exactly what you wanted. First gets the absolute path, then joins folder wanted in the path, and finally creates it if it is not exists.
import os
# Gets current working directory
path = os.getcwd()
# Joins the folder that we wanted to create
folder_name = 'output'
path = os.path.join(path, folder_name)
# Creates the folder, and checks if it is created or not.
os.makedirs(path, exist_ok=True)
Getting help from the answers above, I reached this solution
if not os.path.exists(os.getcwd() + '/' + folderName):
os.makedirs(os.getcwd() + '/' + folderName, exist_ok=True)

error: "Cannot create an existing file" when it does not actually exist

I'm coding a sound recording and analysis program in python but I have a problem with the command (on first run it worked great but since IMPOSSIBLE):
os.mkdir(directory)
As output I get this:
An exception occurred: FileExistsError
[WinError 183] Could not create an already existing file: '\\output'
While in my code I check in the following way in case the folder is indeed already existing:
current_dir = os.getcwd()
# Define a "output" directory :
directory = "\\"+"output"
path = current_dir + directory
isExist = os.path.exists(path) # Valeure booléenne
if not isExist:
# Create the directory
# 'result' in
# current directory
os.mkdir(directory)
I checked with the debugger and the "isExist" variable has the value "False" but when running the program tells me that the folder already exists ??? Weird...
And of course the "output" folder does not appear in the file explorer! Completely illogical...
Note that this code is in a GitHub repository so maybe the error comes from there but the repository does not contain an "output" folder either!
Thanks in advance to anyone who tries to help me.
Ok well I found the solution, in reality you have to replace "directory" by "path" in the command:
os.mkdir(path)
Because otherwise it will only work the first run and even if you manually delete the folder.
So here is the code if it can help someone else!
import os
current_dir = os.getcwd()
# Define a "output" directory :
directory = "\\"+"output"
path = current_dir + directory
isExist = os.path.exists(path) # Valeure booléenne
if not isExist:
# Create the directory
# 'output' in
# current directory
os.mkdir(path)

Creating Directories in Ubuntu with python

I am trying to create directory in Ubuntu using python and save my zip files in it. My code is working fine in windows but behaving weirdly with ubuntu.
import os
import zipfile
import datetime
from os.path import expanduser
home = expanduser('~')
zip_folder = home + '\\Documents\\ziprep' # enter folder path where files are
zip_path = home + '\\Documents\\zips' #enter path for zip to be saved
global fantasy_zip
def dailyfiles(weekly_file,today):
today = str(today)
try:
os.mkdir(zip_path + today)
except OSError:
print("Creation of the directory %s failed" % today)
else:
print("Successfully created the directory %s " % today)
for folder, subfolders, files in os.walk(zip_folder):
for file in files:
if file.startswith(today) and not file.endswith('.zip') and file not in weekly_file:
print("Zipping - Filename " + file)
zip_in = zip_path + today + "\\"
fantasy_zip = zipfile.ZipFile(zip_in + file + '.zip', 'w')
fantasy_zip.write(os.path.join(folder, file),
os.path.relpath(os.path.join(folder, file), zip_folder),
compress_type=zipfile.ZIP_DEFLATED)
fantasy_zip.close()
def main():
weekday = str(datetime.datetime.today().weekday())
today = datetime.date.today().strftime('%Y%m%d')
dailyfiles(weekly_file,today)
if __name__ == '__main__':
main()
Logically it should create a folder with todays date at the path specified. But it is creating Folder in ubuntu with the whole path at the same directory where m script is.
For example it is creating folder with name like this: '/home/downloads/scripypath'
Whereas I need '20191106' at the path which is specified in script. The code is working fine in windows.
Link to current project file
in ubuntu directory structure is totally different and they use \ instead of /.
so prepare your link as ubuntu file structure.
I suggest using home + '/Documents/ziprep/'and home + '/Documents/zips/' on lines 8 and 9, respectively.
EDIT: Sorry, forgot why this should solve the problem. In Linux or Unix, directories use "/" instead of "\" (used in Windows) as directory separators.

Having trouble using python2.7 to access Active Directoy

I tried using Auth0 from Windows, but its only a trial. The GUI application is suppose to have access to the local Active Directory Users and Computers. By use
Which I am trying to figure out the simplest way to implement.
https://docs.python.org/2/library/os.path.html
import os
path = "your path"
# Check current working directory.
retval = os.getcwd()
print "Current working directory %s" % retval
# Now change the directory
os.chdir( path )
# Check current working directory.
retval = os.getcwd()
print "Directory changed successfully %s" % retval

Opening Pathway to Remote Directory (Python)

I am working on a code to copy images from a folder in a local directory to a remote directory. I am trying to use scp.
So in my directory, there is a folder that contains subfolders with images in it. There are also images that are in the main folder that are not in subfolders. I am trying to iterate through the subfolders and individual images and sort them by company, then make corresponding company folders for those images to be organized and copied onto the remote directory.
I am having problems creating the new company folder in the remote directory.
This is what I have:
def imageSync():
path = os.path.normpath("Z:\Complete")
folders = os.listdir(path)
subfolder = []
#separates subfolders from just images in complete folder
for folder in folders:
if folder[len(folder)-3:] == "jpg":
pass
else:
subfolder.append(folder)
p = dict()
for x in range(len(subfolder)):
p[x] = os.path.join(path, subfolder[x])
sub = []
for location in p.items():
sub.append(location[1])
noFold= []
for s in sub:
path1 = os.path.normpath(s)
images = os.listdir(path1)
for image in images:
name = image.split("-")
comp = name[0]
pathway = os.path.join(path1, image)
path2 = "scp " + pathway + " blah#192.168.1.10: /var/files/ImageSync/" + comp
pathhh = os.system(path2)
if not os.path.exists(pathhh):
noFold.append(image)
There's more to the code, but I figured the top part would help explain what I am trying to do.
I have created a ssh key in hopes of making os.system work, but Path2 is returning 1 when I would like it to be the path to the remote server. (I tried this: How to store the return value of os.system that it has printed to stdout in python?)
Also how do I properly check to see if the company folder in the remote directory already exists?
I have looked at Secure Copy File from remote server via scp and os module in Python and How to copy a file to a remote server in Python using SCP or SSH? but I guess I am doing something wrong.
I'm new to Python so thanks for any help!
try this to copy dirs and nested subdirs from local to remote:
cmd = "sshpass -p {} scp -r {}/* root#{}://{}".format(
remote_root_pass,
local_path,
remote_ip,
remote_path)
os.system(cmd)
don't forget to import os,
You may check the exitcode returned (0 for success)
Also you might need to "yum install sshpass"
And change /etc/ssh/ssh_config
StrictHostKeyChecking ask
to:
StrictHostKeyChecking no

Categories