Pwd() cd() and I/O text for open files from pc

Where I’m wrong?

(I’m following on YT Julia for nervous beginners) but now I’ returning in my steps because I met same problem in I/O text files where julia answer me: “no such file or directory” after order fh = open(“fname”)

after writing pwd() Julia write the same directory

cd stands for “change directory”, but you’re passing as argument a path to a file (.jl), not to a directory (cartella).

Restart Julia and try the following:

julia> pwd()
"C:\\Users\\Amministratore"

julia> cd(".\\Julia")

julia> pwd()
"C:\\Users\\Amministratore\\Julia"

julia> io = open("myfile.jl", "w");

julia> write(io, "Hello world!");

julia> close(io);

julia> io = open("myfile.jl", "r");

julia> read(io, String)
"Hello world!"

You can also open the file in a text editor and see for yourself that “Hello world!” was written.

Finally, check out this topic: Please read: make it easier to help you.

3 Likes

Hey, thank you for your time that are you helping me, but seems that’s not working.

i read from others forums that should be a Julia bug, but the first time that i did work well.

I don’t know what to do.

you don’t cd into a FILE, you only cd into a directory (folder). myfile.jl is a file, you can’t cd into it

3 Likes

As @jling mentioned you can’t cd into a file. To get the parent folder from a file path you can do the following:

filepath = raw"C:\Users\Amministratore\Julia\myfile.jl"
parent_dir = dirname(filepath)
cd(parent_dir)

You can also check if the path is valid by using isfile,ispath or isdir

ispath(raw"C:\Users\Amministratore\Julia\myfile.jl")
isfile(raw"C:\Users\Amministratore\Julia\myfile.jl")
isdir(raw"C:\Users\Amministratore\Julia\myfile.jl")
3 Likes

thanks, give me “false” :man_shrugging:
ispath–> false
isfile–>false
isdir–>false LOL

even
include("myfile.jl") give me no such file or directory

Users not User

you can use <tab> complete the path as you’re typing it

3 Likes

What do you get when executing this?

c = raw"C:\Users\Amministratore\Julia\myfile.jl"
ispath.(accumulate(joinpath,splitpath(c)))
1 Like

manipulated? (julia libraries?)

because the first time was ok…

Ok, so you don’t have a file called myfile.jl in C:\Users\Amministratore\Julia

1 Like

yeah but the screenshot confirm it LOL

so this should give you all the files in the Julia folder

c = raw"C:\Users\Amministratore\Julia\myfile.jl"
cd(dirname(c))
readdir()
1 Like

exactly give me correct all them

3-element Vector {String}: myfile.jl.txt new1.jl newfile.jl.jl

already done all things with others 2 .jl

So you created a new file in windows while the file explorer was hiding the filename extensions.
The file is called myfile.jl.txt instead of myfile.jl

1 Like

already done with others 2 same things that you suggested, doesn’t change nothing

ispath(raw"C:\Users\Amministratore\Julia\newfile.jl") false

isfile and others… i tried all

To read the content of the file you can run this

c = raw"C:\Users\Amministratore\Julia\myfile.jl.txt"
data = open(c,"r") do io
  read(io,String)
end
@show data
1 Like

from data give me error, goodbye.

I leave (even with others files in the folder instead of myfile, like newfile, new1 (in jl) )

ok, so you need to: https://www.howtogeek.com/205086/beginner-how-to-make-windows-show-file-extensions/

again, this is why I asked you to use tab to complete the path, so you know what exactly IS in there.

4 Likes

don’t exagerate! i got the same problem with another individual between C: and D: directory so I created another folder in external drive and resolved in this mode. Ok. I can go on :joy:

It’s resolved after also uninstalled/re-installed annnddd " it’s very important.

Thank’s to all. I love YOU ALL for the patience, I know tutor was a better choice…