www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why file.exists of relative path on Linux always return false?

reply Suliman <evermind live.ru> writes:
I am trying to check relative path on Linux for exists.

import std.stdio;
import std.path;
import std.file;
import std.string;



string mypath = "~/Documents/imgs";

void main()
{
  if(!mypath.exists)
	{
		writeln(mypath, " do not exists");		
	}

  if(!mypath.exists)
	{
		writeln(mypath, " do not exists");		
	}

  if("/home/dima/Documents/imgs".exists)
	{
		writeln("/home/dima/Documents/imgs");
		writeln("Dir exists");
	}

}

~/Documents/imgs always return "do not exists". But full path: 
"/home/dima/Documents/imgs" is "Dir exists".

Why? It's same paths!
Feb 29 2016
parent reply Alex Parrill <initrd.gz gmail.com> writes:
On Monday, 29 February 2016 at 14:50:51 UTC, Suliman wrote:
 I am trying to check relative path on Linux for exists.

 import std.stdio;
 import std.path;
 import std.file;
 import std.string;



 string mypath = "~/Documents/imgs";

 void main()
 {
  if(!mypath.exists)
 	{
 		writeln(mypath, " do not exists");		
 	}

  if(!mypath.exists)
 	{
 		writeln(mypath, " do not exists");		
 	}

  if("/home/dima/Documents/imgs".exists)
 	{
 		writeln("/home/dima/Documents/imgs");
 		writeln("Dir exists");
 	}

 }

 ~/Documents/imgs always return "do not exists". But full path: 
 "/home/dima/Documents/imgs" is "Dir exists".

 Why? It's same paths!
~ is expanded by your shell. It is not a relative path, and system calls do not recognize it (same with environmental variables). See also http://stackoverflow.com/questions/3616595/why-mkdir-fails-to-work-with-tilde
Feb 29 2016
parent Edwin van Leeuwen <edder tkwsping.nl> writes:
On Monday, 29 February 2016 at 14:58:46 UTC, Alex Parrill wrote:
 On Monday, 29 February 2016 at 14:50:51 UTC, Suliman wrote:
 I am trying to check relative path on Linux for exists.

 string mypath = "~/Documents/imgs";
~ is expanded by your shell. It is not a relative path, and system calls do not recognize it (same with environmental variables).
D can expand tilde with expandTilde: import std.path : expandTilde; string mypath = expandTilde("~/Documents/imgs");
Feb 29 2016