backslash removal while constructing absolute path - concatenation

I want to construct an absolute path with '.zip' in the end, Following is the snippet,
path='folder/file'
absPath=os.path.join('E:/',path,'.zip')
I am getting the following output for this,
E:/folder1/file\.zip
Required output,
E:/folder/file.zip
How can I avoid the \ before .zip while concatenating??

Are you building paths for windows? Seemingly so because you use a drive letter at the start and because os.path.join is using backslashes to build the path. If so, you are going to have problems because you are using forward slashes in your input path.
os.path.join() works by taking the arguments provided and putting the proper separator between them. If you call .zip as an argument it will get a seperator before it which isn't what you want. What you probably want to say is this:
# folder1 and folder2 are presumably variables you are bringing in
abspath = os.path.join('E:\\', folder1, folder2 + '.zip')
If you go your way, or the way recommended in some answers, then you might as well just concatenate the strings together. If you actually want the path to show as "E:/folder1/folder2.zip" and folder1 and folder2 are known, you can easily do:
abspath = "E:/{0}/{1}.zip".format(folder1,folder2)
The only thing os.path.join does for you is put in the correct path separator for the OS you are running on. If you don't want that functionality, using it to concatenate a string is just unnecessary overhead.

I would recommend you to avoid calling that folder2 a folder :) seems its the filename, and you add the .zip to that.
I would do something like this
import os
dir_base="E:/"
dir_name='folder1'
base_filename='folder2'
filename_suffix = 'zip'
os.path.join(dir_base + dir_name, base_filename + "." + filename_suffix)

While joining one or more paths, os.path.join will add one directory separator to each arguments it takes. For more Refer docs
As zip is not a directory it should include with the file name. Hence, string concatenation should be good instead of os.path.join
Updated code looks like this:
import os
path='folder1/folder2'
absPath=os.path.join('E:/',path+'.zip')
print(absPath)
OR
import os
path='folder1/folder2.zip'
absPath=os.path.join('E:/',path)
print(absPath)
There are lot of ways to write this!

Related

I want to replace a lot of files with differing names with the same file without changing the name, how can I do that?

I have a bunch of .txd and .dff files that have different names, let's say, like, img1 img30 greenimg3, etc. all in the same folder. I can compile a list of these names that I want to replace (but not all the files in the folder will be replaced), but how can I replace my list of files with just one file without altering the name of the file? So it remains img1, img30, greenimg3, etcetera but with the replaced file. I hope this makes sense. I would be grateful if someone could write something that I could use, as I don't know how to code myself.
not sure if i understand corretly, but you could do this in python:
import os
folder_path = "./folder"
with open("source.file", "rb") as source_file:
source_file_content = source_file.read()
for file in os.listdir(folder_path):
with open(os.path.join(folder_path, file), "wb") as destination_file:
destination_file.write(source_file_content)
where "./folder" is the path to your folder containing all files you want to overwrite and "source.file" is the path to your file you want to overwrite with.
this script reads and writes the files as binary

Does "./..." mean all subfolders?

I often see "./..." in makefile. I think it means it is all subfolders in the current directory. Could someone confirm and provide me the source where this syntax is explained?
Example:
go generate ./...
The import path pattern ./... matches all packages in directories below the current directory, except those in vendor directories.
The pattern is implemented by the go tool. The pattern is not interpreted by make, bash and any other tool that might invoke the go tool.
The documentation for the go command says:
An import path is a pattern if it includes one or more "..." wildcards, each of which can match any string, including the empty string and strings containing slashes. Such a pattern expands to all package directories found in the GOPATH trees with names matching the patterns.
To make common patterns more convenient, there are two special cases. First, /... at the end of the pattern can match an empty string, so that net/... matches both net and packages in its subdirectories, like net/http. Second, any slash-separated pattern element containing a wildcard never participates in a match of the "vendor" element in the path of a vendored package, so that ./... does not match packages in subdirectories of ./vendor or ./mycode/vendor, but ./vendor/... and ./mycode/vendor/... do. Note, however, that a directory named vendor that itself contains code is not a vendored package: cmd/vendor would be a command named vendor, and the pattern cmd/... matches it. See golang.org/s/go15vendor for more about vendoring.
and it also says:
An import path beginning with ./ or ../ is called a relative path. The toolchain supports relative import paths as a shortcut in two ways.
First, a relative path can be used as a shorthand on the command line. If you are working in the directory containing the code imported as "unicode" and want to run the tests for "unicode/utf8", you can type "go test ./utf8" instead of needing to specify the full path. Similarly, in the reverse situation, "go test .." will test "unicode" from the "unicode/utf8" directory. Relative patterns are also allowed, like "go test ./..." to test all subdirectories. See 'go help packages' for details on the pattern syntax.
Second, if you are compiling a Go program not in a work space, you can use a relative path in an import statement in that program to refer to nearby code also not in a work space. This makes it easy to experiment with small multipackage programs outside of the usual work spaces, but such programs cannot be installed with "go install" (there is no work space in which to install them), so they are rebuilt from scratch each time they are built. To avoid ambiguity, Go programs cannot use relative import paths within a work space.

curl output file replacing path seperator with underscore when writing the file

I'm trying to create a fairly simple batch file to download a file from our ftp site and store it in a specific directory. I would like to be able to put the batch file in the path so I can call it from anywhere. Currently I have a line like the following:
curl -v -u %FTP_USER%:%FTP_PASS% -Q "TYPE I" -o %OUTPUT_PATH% "ftp://%FTP_HOST%/%JAR_FILE_NAME%"
What I've discovered is that no matter the value of the output path, the file is always written in the current directory, and any path seperators are converted to underscores in the file name. This happens no matter if I use a '\' or '/' and if I try to escape it with double slashes I just end up with two underscores. Quotes around things don't seem to help either.
My question is does the -o option allow for outputting to a folder other than current working dir? I guess I can have the next step in the script to be "move the file to its destination", but that seems really kludgey.
This sounds exactly like a regression (reported here) we unfortunately brought in curl 7.47.0. We hope to release an updated, fixed, version soon and in the mean time you can probably consider downgrading to an earlier version to work-around this annoying issue.

How to copy the names of the subfolders in a folder, so to use them in a batch program (command line)?

I need to check the files of a versioned system. To do that, I need to write a batcha program so to compare the contents of several folders containing the repositories.
So, my question is: how can I "read" the names of all the subfolders inside a folder, so to use these names later to find subfolders having the same names in a different repositories?
I suppose I may use DIR to print on the screen a list of these names but I don't know how to write it on a text file and then read it. Moreover, I should edit this kind of list, anyway.
Any suggestions or new ideas to solve this problem?
I thank gratefully who ever will answer.
it seems that you can get the subfolders using batch file from perl as follows:
system("start C:\\Temp\\mybatchfile.bat");
or you might try to pass your command suggested by #Stephan straight to system and try to handle what it is returned.

Batch file to move a number of files, then replace each of them with one file of another type while letting them keep their original name

First off I want to say that
-I didnt ever create a batch file yet, but I am really willing to learn
-I am not even sure if what i want to do is possible with a batch file
What i want to do is the following:
I want to replace a number of files of one file type in a folder each with one and the same file of another file type. In doing this, i want the "replaced" files to keep their original name except for the "replacer" file's extension. I am not talking about file conversion, this is about replacing several different files each with one and the same file, so each of them will look the same later, just with different names and the file extension of the "replacer" file. All of the files inside the folder are to be treated this way, there are no exceptions.
So it looks something like this:
Folder 1 Folder 2
10000000.tga------------->10000000.png (looks like replacer.png)
10000001.tga------------->10000001.png (looks like replacer.png)
10000011.tga------------->10000011.png (looks like replacer.png)
I really hope that my description is sufficiently precise, if not so, I am of course willing to give any information needed. I found parts of what i need (e.g. a loop for files in a folder, an order to replace one file with another file) but I am unsure of how to combine them at all, let alone to achieve what I actually wanted to do.
Any help is greatly appreciated :)
for %%i in (*.tga) do (
copy "replacer.png" "%%~ni.png"
del "%%i"
)
see for /? for details about the %%~.. syntax

Resources