If I want to write a file to C:/output/results.csv what's a simple way to make the directory if it doesn't exist? I want to do this because CSV.write(path,data) errors if C:/output/ doesn't exist.
mkdir errors if the directory already exists. I am currently doing the following, but is there a safer/cleaner way to do this?
try
mkdir("C:/output")
catch
# if errors, likely already exists
end
Edit:
As one of the commenters pointed out, mkpath will create a directory if it doesn't exist, and in either case will return the directory name.
My question was confounding the usage of mkdir (which errors if directory exists) and mkpath which does not error in that case.
You could explicitly check whether the directory exists beforehand using isdir:
isdir(dir) || mkdir(dir)
CSV.write(joinpath(dir, "results.csv"), data)
But this will not necessarily handle all corner cases, like when the path already exists but is a link to another directory. The mkpath function in the standard library should handle everything for you:
mkpath(path)
CSV.write(joinpath(path, "results.csv"), data)
mkpath(oath) will create a directory if it does not exist and return the path after doing so. If it already exists, the path is returned.
Related
I'm sorry if this is basic and I missed something simple. I'm trying to run the code below to iterate through files in a folder and merge all files that start with a specific string, into a dataframe. All files sit in a lake.
file_list=[]
path = "/dbfs/rawdata/2019/01/01/parent/"
files = dbutils.fs.ls(path)
for file in files:
if(file.name.startswith("CW")):
file_list.append(file.name)
df = spark.read.load(path=file_list)
# check point
print("Shape: ", df.count(),"," , len(df.columns))
db.printSchema()
This looks fine to me, but apparently something is wrong here. I'm getting an error on this line:
files = dbutils.fs.ls(path)
Error message reads:
java.io.FileNotFoundException: File/6199764716474501/dbfs/rawdata/2019/01/01/parent does not exist.
The path, the files, and everything else definitely exist. I tried with and without the 'dbfs' part. Could it be a permission issue? Something else? I Googled for a solution. Still can't get traction with this.
Make sure you have a folder named "dbfs" if your parent folder starts from "rawdata" the path should be "/rawdata/2019/01/01/parent" or "rawdata/2019/01/01/parent".
The error is thrown in case of incorrect path.
This is an old thread, but if someone is still looking for a solution:
It does require path to be listed as:
"dbfs:/rawdata/2019/01/01/parent/"
How can I rename the folder called «grandparent» in the following directory structure
C:\Temp\grandparent\parent\child\
to «GrandParent» and
C:\Temp\GrandParent\parent\child\
The child-folder is not empty and contains different files.
If I try it with the command IO.Directory.Move(OldDirName, NewDirName) I get an error that the directory name must not be identical (ignoring the different case spelling).
Do I really have to move it to a completely new temporary directory (like C:\AnotherTemp\ and then move it back to the desired «GrandParent»?
(This answer seems only to work, if the last part («child») should be renamed.)
i try to rename a file using vb.net in this way:
my.computer.filesyste.rename(oldname,newname)
But if i use a software to recover files deleted, i find a file named :"_ldname", and if i recovery the file "_ldname" i have, in this way, two files equals.
Can i do this without have a duplicate of my file?
Best regards
Sebastiano
You cannot, this is a windows filesystem limitation and nothing to do with programming. Two files cannot have the same name in the same location.
The recovery software should be forcing a rename to Myfile(1).txt or something like that to distinguish between the two files.
You could always use:
If File.Exists(path) = False Then
To make sure the file doesn't already exists. Then if it does exist you could add a "(1)" to the file name.
I have a Matlab program that does something like this
cd media;
for i =1:files
d(i).r = %some matlab file read command
d(i).process();
end
cd ..;
When I change to my "media" directory I can still access member properties (such as 'r'), but Matlab can't seem to find functions like process(). How is this problem solved? Is there some kind of global function pointer I can call? My current solution is to do 2 loops, but this is somewhat deeply chagrining.
There are two solutions:
don't change directories - instead give the file path the your file read command, e.g.
d(i).r = load(['media' filesep 'yourfilename.mat']);
or
add the directory containing your process() to the MATLAB path:
addpath('C:\YourObjectsFolder');
As mentioned by tdc, you can use
addpath(genpath('C:\YourObjectsFolder'));
if you also want to add all subdirectories to your path.
Jonas already mentioned addpath, but I usually use it in combination with genpath:
addpath(genpath('path_to_folder'));
which also adds all of the subdirectories of 'path_to_folder' as well.
I have the following bat but it dosnt seem to work I want to check for a file name stoted in tom.txt, if it exists i want to do nothing, if however it dont exist i want to run the runme.bat
Echo Setting variable to file name
set FAT=<C:\tom.txt
ECHO Checking for file, if exists do nothing if not run bat...
if exists %FAT% (
end
)else(
C:\runme.bat
)
There are some minor mistakes, which, however, are the cause of your major difficulties.
You can read a line from a file with the SET /P command, not simply with SET:
SET /P FAT=<C:\tom.txt
The keyword in the file existence check command is EXIST, not EXISTS
IF EXIST …
Also, if you only need to react to the file's non-existence, you can simply add NOT:
IF NOT EXIST …
So, the entire command might be like this:
IF NOT EXIST %FAT% C:\runme.bat
I believe the correct syntax is
if exist %FAT% goto NORUN
C:\runme.bat
:NORUN
Notice "exist" vs "exists" in your code. A couple other things to note:
File NUL exists in any directory on any drive, therefore checking
for C:\NUL will always return true.
Checking for file existence
does not always work correctly on network devices.
See http://support.microsoft.com/kb/65994 for a bit more information.
I think the simplest solution would be to do it all on one line like this:
IF NOT EXIST C:\tom.txt C:\runme.bat
There is no need for the variable unless you intend on using it again, it just means another line of code. As Aleks G and Andriy M said, you need to make sure the commands and parameters are spelt correctly.