If I am given a path ("C:\Users\Meow\Desktop"), how do I get a relative path from a user’s normal cd input? For example:
func("C:\Users\Meow\Desktop","../../")
returns "C:\Users"
And other similar things. Basically simulating the behaviour of a cd command on Windows, without access to that specified file system
Try this:
os.path.normpath(os.path.join("C:\\Users\\Meow\\Desktop","../../"))
Note that the \ in the first path needs to be escaped, i.e. \\.
Related
For some reason when I run this in my shell script only $exec_file and $_dir get passed into module_test.py, but neither $filename nor $modified_file get passed.
mod_test=$( /path/module_test.py $modified_file $_dir $filename )
(path is just a normal path that I decided to shorten for the sake of this example)
Am I typing this wrong? I am trying to get the output (an integer) of my module_test.py to be put into the variable mod_test.
My variables are:
modified_file = _File
_dir = /path to directory/
file = _File.py
Based on your example, you need to surround $_dir with quotes because it contains spaces, i.e.:
mod_test=$( /path/module_test.py $modified_file '$_dir' $filename )
I'm running into problems with the difference in environment between SConscripts and custom tools under site_tools.
I'm trying to find the absolute path of a string, which is fine in SConscript:
print File('#something').get_abspath()
>> /home/projects/something
Similar code in site_tools:
from SCons.Node.FS import File
print File('#something').get_abspath()
>> TypeError: __init__() takes exactly 4 arguments (2 given)
So clearly the File() constructed in SConscript is not a SCons.Node.FS.File.
So the questions are:
1) What is the File object in a SConscript, and how can I get one in a builder/site_tools?
2) How can I get the absolute path in site_tools? I need this because I'm constructing command-line arguments for tools which need absolute paths.
This looks like an XY problem to me. If you need the absolute path of single files or directories in your command lines, the construction of the full path should be left to SCons itself.
Please check out the MAN page (http://scons.org/doc/production/HTML/scons-man.html or "man scons") under the section "Variable Substitution". You're able to append further specifiers (like abspath in your case) to variables, and they get substituted before command execution.
So the command line string for your Builder action could look something like this:
$MYTOOL -i $SOURCE.abspath $TARGET.abspath
Is the path separator employed inside a Python tarfile.TarFile object a '/' regardless of platform, or is it a backslash on Windows?
I basically never touch Windows, but I would kind of like the code I'm writing to be compatible with it, if it can be. Unfortunately I have no Windows host on which to test.
A quick test tells me that a (forward) slash is always used.
In fact, the tar format stores the full path of each file as a single string, using slashes (try looking at a hex dump), and python just reads that full path without any modification. Likewise, at extraction time python hard-replaces slashes with the local separator (see TarFile._extract_member).
... which makes me think that there are surely some nonconformant implementations of tar for Windows that create tarfiles with backslashs as separators!?
I'm building out a powershell script that does quite a bit of work extracting csv's from a zip, converting OS->lat/lon, stuffing the contents robustly into a DB, and then emailing a distribution list with stats on the whole process.
Most of this is now complete, but to make the whole thing a little more portable I'm providing paths to input/working/output folders as parameters of the powershell call from a batch file.
This is all working fantatstically until I need to call python scripts to do the lat/lon work, as passing in the variable parameter paths doesn't seem to work with any permutation/combination.
The following is a simplified version of the python path within the .PS1 script which works from both command prompt and from within the .PS1 file if called directly (where -i -o are input/output path parameters).
c:\python27\python.exe D:\PythonPPC\subs.py -i D:\PPC\subs_export.csv -o D:\PPC\subs_export_lat_lon.csv
In my script I would like to replace the two path parameters -i/-o with variables something like:
c:\python27\python.exe D:\PythonPPC\subs.py -i $inputPathsubs_export.csv -o $outputPathsubs_export_lat_lon.csv
Does anyone have any idea on how to invoke this command as I've tried the &$exe method described on stack and a few other places, but this simply results in the error shown below:
CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
Any help would be greatly appreciated.
I'd recommend using the Join-Path cmdlet for building paths. It will save you the headache of keeping track of leading/trailing path separators and also canonicalize / to \.
$workingFolder = 'C:\some/where\'
$extractFolder = '\extract\folder'
$infileName = '/subs_export.csv'
$outfileName = '\subs_export_lat_lon.csv'
$infile = Join-Path (Join-Path $workingFolder $extractFolder) $infileName
$outfile = Join-Path (Join-Path $workingFolder $extractFolder) $outfileName
Result:
PS C:\> $infile
C:\some\where\extract\folder\subs_export.csv
PS C:\> $outfile
C:\some\where\extract\folder\subs_export_lat_lon.csv
If you still want to simply concatenate strings to a path, you can separate variables from trailing strings by putting the variable name between curly brackets. In your example:
python.exe D:\PythonPPC\subs.py -i "${inputPath}subs_export.csv" ...
or
python.exe D:\PythonPPC\subs.py -i "${rootFolder}${subFolder}subs_export.csv" ...
After attacking the problem from a different direction and a colleague pointing out my own abject stupidity, it seems the following is a fairly straightforward solution to the complicated problem above.
$inputFilePath = $pathToWorkingFolder + $pathToExtractFolder + 'subs_export.csv'
$outputFilePath = $pathToWorkingFolder + $pathToExtractFolder +'subs_export_lat_lon.csv'
c:\python27\python.exe D:\PythonPPC\subs.py -i $inputFilePath -o $outputFilePath
Just needed some peer review I guess...
To fix a problem in code for work, I was told to "use a path relative to ~". What does ~ mean in a file path? How can I make a path that is relative to ~, and use that path to open files in Python?
it is your $HOME var in UNIX, which usually is /home/username.
"Your home" meaning the home of the user who's executing a command like cd ~/MyDocuments/ is cd /home/user_executing_cd_commnd/MyDocuments
Unless you're writing a shell script or using some other language that knows to substitute the value of $HOME for ~, tildes in file paths have no special meaning and will be treated as any other non-special character.
If you are writing a shell script, shells don't interpret tildes unless they occur as the first character in an argument. In other words, ~/file will become /path/to/users/home/directory/file, but ./~/file will be interpreted literally (i.e., "a file called file in a subdirectory of . called ~").
Used in URLs, interpretation of the tilde as a shorthand for a user's home directory (e.g., http://www.foo.org/~bob) is a convention borrowed from Unix. Implementation is entirely server-specific, so you'd need to check the documentation for your web server to see if it has any special meaning.
If you are using pathlib for filenames then you can use on both Windows and Linux (I came here for a windows answer):
from pathlib import Path
p = Path('~').expanduser()
print(p)