Emacs home directory - file

Many commands access files, and they usually take a path relative to ~/ which I gather is the "home" directory. In my case, C:\Users\Grant\AppData\Roaming\ which is where .emacs.d\ and my init files live.
This is great for Emacs, but not as useful for me. I typically would be over in C:\Users\Grant\Documents\ or my Python script files directory.
Also, I'm using Customize and typing in absolute file names for things like Org capture templates (currently anything I capture goes into files in one particular directory). Not that it's likely that these locations will change, but I was wondering if there are variables I can set for these places, which I can use in Customize or when invoking Dired - maybe some sort of environment variable? And if so, how can I refer to them when typing in a file name?

Related

Do you need to be specific about a file location when using os or do you still ned to write (folder/file)?

Let's say I have a file called hello.txt in the folder called coding, and I want to open that in python. I know that if I don't use os, I would have to write open("coding/hello.txt") but if I would write os.open would I still have to specify the folder like ("coding/hello.txt") or can I just write os.open("hello.txt") because I am using os?
"File" and "operating system" can mean a lot of different things, but typically operating systems have the concept of a "current" or "working" directory. Each process has its own current directory, and if you don't specify a directory for a file it uses the current directory.
Do not rely on this. Too many things can change the current directory unexpectedly, and your program will suddenly start using a different file.
Instead always specify the full file path like open("/usr/tmp/coding/hello.txt") or whatever is appropriate for your operating system; it will probably provide environment variables or something for the user's home or temporary directories.
Note that your examples "coding/hello.txt" and "hello.txt" both use the current directory, and are different files.

Finding file locations in offline softwares in C

At some point in my C program I have to deal with something like this.
FILE * fptr = fopen("/Parent/child/.../file.dat");
Which means in order to access any file I need to know it's location. That's all understandable.
But, how can I make this generic? In my computer "/Parent/child/.../file.dat" will work because that's where the file is stored, but I'm making a software to distribute to other users so the path obviously differs. My question is, how can I install a specific file into the user's computer such that I can know and get the location of that file. I a but confused about this concept so any resources that could help me understand it better would be greatly appreciated.
In Linux the default path to application files should be hardcoded. There is a standard which applications should follow. For example, architecture-independent files should go to /usr/share/ and then either your application name or, if you expect the data to be shared between applications, a generic category such as images. User-specific configuration files should go $HOME/.config/<app-name>. Older applications place their default configuration in $HOME/.<app-name> instead.
You should also provide an ability to override the default path to the data with a command line switch and/or an environment variable and/or a user configuration file (the location of the latter should also be overridable with a command line switch and/or an environment variable).
Some applications search for their data directory relatively to the executable position. An executable can know its own absolute path by reading /proc/self/exe symbolic link. For example, if an executable finds itself in /usr/local/bin/somename, it can look for /usr/local/share/<app-name> (two levels up from the executable name and down to share/<app-name>.).
Finally, if you distribute source code for the users to build, the file locations should be configuration parameters.

How to open file with path relative to c file, not working directory

I'm modifying a large codebase written in C. This code is designed to be compiled and ran from an arbitrary working directory. This is so configuration files can be read and output written to/from the working directory, making it easier to organize the setup and outputs of the code.
The additions I've made to this code need to read data from a few data files. I would like to place these in the same directory as the .c file where they are read, with a directory structure as follows:
big_project/
|-- models/
| |-- my_file.c
| |-- my_data.txt
My problem comes in when trying to open this data file using relative paths. Typically relative paths are relative to the working directory, which would not work in my case since the working directory can be arbitrary. From inside my_file.c, how can I open my_data.txt for reading using relative paths?
Based on our conversation in the comments, you have several alternatives, which I'll list below from, IMHO, most to least desirable.
You never specified if you were in Windows, GNU+Linux, or were doing cross-platform development, but I'm sure you can adapt the suggestions to your platform.
Multiple and Custom Config Files (Recommended)
You could modify your program to look at your platform's standard location for program data and/or configuration files. For example, you could have it look for a standard config at /etc/<your-program>/default.conf in GNU+Linux or %APPDATA%\<your-program>\default.conf in Windows.
If different users need to use their own personal configs, the program could also be made to accept a config file path as an argument. For example:
GNU+Linux:
$ ./your-program --config ${HOME}/.your-program/my.conf
Windows:
> your-program.exe --config %userprofile%\your-program\my.conf
Note that the use of %userprofile% may change based on Windows versions and/or shells used (e.g. standard cmd.exe vs powershell).
Compiling in the Path (Not Recommended)
Based on your comments, a short-term workaround could be to compile the absolute path into it for the __FILE__ macro to give that back to you at runtime. As I said in my comment:
if you're completely sure about the program always being placed in the same directory for everyone, then you can set the absolute path shown by the __FILE__ macro if you send the full path when compiling; e.g. gcc $(pwd)/your-file.c, when it prints __FILE__ will show the full path it had at compile time, not run-time. (Can't add enough disclaimers here, though)
Please note that there're many reasons to not use this approach. I'm simply suggesting it as a short-term workaround to pull out of an existing crisis-level situation you may have, while you (hopefully) take a closer look at the more desirable approach to handle configurations and path-finding.

Directory path which does not physically exist on my device

I am learning Python programming language. Currently
I am experimenting i-o files. I imported sys module and
in sys.path list I saw two kinds of paths:
/data/data/org.qpython.qpy3....
/storage/sdcard0/qpthon...
The former path does not exist physically on my device
(Tablet), although I can create/read files using this
path through python.
I want to know about these paths.
What are they called?
What are they for? etc.
The first path, /data/data/org.qpython.qpy3, this is where the actual QPython app is stored on your device. I don't believe you can access this path without having root access.
The second path, /storage/sdcard0/qpthon, this is where QPython saves files by default. It uses this location because it can be easily accessed with normal user privileges.

How link to any local file with markdown syntax?

I have a local markdown file containing several links and I want that links head to local file like pdf.
I use the following syntax:
[my link](file:///C:/my_file.pdf)
But when I open my markdown file into a Firefox page and click on the link, nothing happens.
What exactly have I missed? Is it possible to open local file?
None of the answers worked for me. But inspired in BarryPye's answer I found out it works when using relative paths!
# Contents from the '/media/user/README_1.md' markdown file:
Read more [here](./README_2.md) # It works!
Read more [here](file:///media/user/README_2.md) # Doesn't work
Read more [here](/media/user/README_2.md) # Doesn't work
How are you opening the rendered Markdown?
If you host it over HTTP, i.e. you access it via http:// or https://, most modern browsers will refuse to open local links, e.g. with file://. This is a security feature:
For security purposes, Mozilla applications block links to local files (and directories) from remote files. This includes linking to files on your hard drive, on mapped network drives, and accessible via Uniform Naming Convention (UNC) paths. This prevents a number of unpleasant possibilities, including:
Allowing sites to detect your operating system by checking default installation paths
Allowing sites to exploit system vulnerabilities (e.g., C:\con\con in Windows 95/98)
Allowing sites to detect browser preferences or read sensitive data
There are some workarounds listed on that page, but my recommendation is to avoid doing this if you can.
You link to a local file the same way you link to local images. Here is an example to link to file start_caQtDM_7id.sh in the same directory as the markdown source:
![start_caQtDM_7id.sh](./start_caQtDM_7id.sh)
After messing around with #BringBackCommodore64 answer I figured it out
[link](file:///d:/absolute.md) # absolute filesystem path
[link](./relative1.md) # relative to opened file
[link](/relativeToProject.md) # relative to opened project
All of them tested in Visual Studio Code and working,
Note: The absolute and relative to opened project path work in editor but don't work in markdown preview mode!
If you have spaces in the filename, try these:
[file](./file%20with%20spaces.md)
[file](<./file with spaces.md>)
First one seems more reliable
This is a old question, but to me it still doesn't seem to have a complete answer to the OP's question. The chosen answer about security being the possible issue is actually often not the problem when using the Firefox 'Markdown Viewer' plug-in in my experience. Also, the OP seems to be using MS-Windows, so there is the added issue of specifying different drives.
So, here is a little more complete yet simple answer for the 'Markdown Viewer' plug-in on Windows (and other Markdown renderers I've seen): just enter the local path as you would normally, and if it is an absolute path make sure to start it with a slash. So:
[a relative link](../../some/dir/filename.md)
[Link to file in another dir on same drive](/another/dir/filename.md)
[Link to file in another dir on a different drive](/D:/dir/filename.md)
That last one was probably what the OP was looking for given their example.
Note this can also be used to display directories rather than files.
Though late, I hope this helps!
Thank you drifty0pine!
The first solution, it´s works!
[a relative link](../../some/dir/filename.md)
[Link to file in another dir on same drive](/another/dir/filename.md)
[Link to file in another dir on a different drive](/D:/dir/filename.md)
but I had need put more ../ until the folder where was my file, like this:
[FileToOpen](../../../../folderW/folderX/folderY/folderZ/FileToOpen.txt)
If the file is in the same directory as the one where the .md is, then just putting [Click here](MY-FILE.md) should work.
Otherwise, can create a path from the root directory of the project. So if the entire project/git-repo root directory is called 'my-app', and one wants to point to my-app/client/read-me.md, then try [My hyperlink](/client/read-me.md).
At least works from Chrome.

Resources