SoFunction
Updated on 2024-10-29

python Adds a valid path (single or double backslash) to the right-click menu for copying the target file.

Add a valid path to the copy target file in the context menu (single or double backslash)

introductory

As win10 computer comes with get file path as a single backslash enclosed in double quotes as shown below.

在这里插入图片描述

"D:\"

However, when reading a file in many programs, the backslash in the path of the file is used as a special symbol for decompilation, so it is not possible to read the path directly.

For example, in R, reading data can only be done with a slash "/", or a double backslash "\\".

read_csv是readr包中的函数,是原生自带的读取文件的函数,均会报错

It's a pain in the ass to read the table in R every time, either you have to enter it by hand, or you have to use the path that comes with the system and then add backslashes one by one.

As lazy as I am, I can't stand this kind of mechanical maneuvering.
So the road to copying file paths for remodeling was started.

The picture below shows the finished product:

get_path_1 returns the path with a double backslash "\\".
get_path_2 will return the path with a single slash "/"

在这里插入图片描述

1. Python code to get the specified file path and copy it to the system pasteboard

The code returns the path with double quotes and does both types of paths, and copies the result directly to the system's pasteboard after running it.

pwd_1Returns a double backslash path:
"D:\\files\\"

pwd_2Returns a single-slash path:
"D:/files/"

# -*- coding: utf-8 -*-
"""
Created on Fri Sep 27 10:18:43 2019
author: Irvinfaith
email: Irvinfaith@
"""
import sys
import subprocess
class get_pwd():
 """
 Main class to pass a path of target file to console.
 """
 def __init__(self, path):
  = path
 def pwd_1(self):
 """
 Return path splits with double backslash.
 """
 path_1 = ('\\', '\\\\')
 return self.copy_to_clipboard(path_1)
 def pwd_2(self):
 """
 Return path splits with slash.
 """
 path_2 = ('\\', '/')
 return self.copy_to_clipboard(path_2)
 def copy_to_clipboard(self, txt):
 """
 Copy path and add double quote into clipboard.
 """
 cmd = 'echo "' + () + '"|clip'
 return subprocess.check_call(cmd, shell=True)
if __name__ == '__main__':
 gp = get_pwd([2])
 if [1] == 'path_1':
 gp.pwd_1()
 elif [1] == 'path_2':
 gp.pwd_2()
 else:
 pass

2. Save the script and run it in the terminal test.

Save the script here as "
Then open cmd into the file storage path test run

在这里插入图片描述

The first parameter "path_2" is the type of the path to be obtained, i.e., a single-slash path, and the second parameter is the file for which a valid path is to be obtained.
Running it without reporting any errors means that the valid path to the file has been copied to your pasteboard.

Press ctrl+v anywhere to test it, and it pastes out as follows:

"D:/"

The test was successful and it's time to start revamping the right-click menu.

3. Edit .bat batch files and .reg registry files

Since there are two types of paths, I am putting both in the context menu, the

在这里插入图片描述

So you need two bat and reg files, here you can just save the file according to your actual needs, depending on the type of path you need and enter the specified parameters.
Only one of the parameters is shown here aspath_1As an example, the other one is just to change the execution parameters in the bat file, and change the name in the right-click menu in the reg file, but everything else is the same.

3.1 .bat batch file

existC:\Windowsdirectory, create a new blank file and enter the following:

D:\software\anaconda\ D:\ path_1 %*

Here the first parameterD:\software\anaconda\is an executable program for python on the system
second parameterD:\is the script path
Third parameterpath_1which is the path type of the file to be returned.
Notice it's followed bypercent sign % (punct.)cap (a poem)asterisksDon't miss it.%*

After editing save as "get_file_wd_path_1.bat

3.2 .reg registry files

existC:\Windowsdirectory, create a new blank file and enter the following:

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\get_path_1\command]
@="get_file_wd_path_1.bat \"%1\""

where the second line of theget_path_1For the text displayed in the context menu
The third line of "get_file_wd_path_1.bat" is the name of the .bat file
Nothing else moves to copy and duplicate.

Save after editing"get_file_wd_path_1.reg"

4. Run the .reg file to add the information to the registry

Double-click to run the just-saved "get_file_wd_path_1.reg", click "Yes".

在这里插入图片描述
在这里插入图片描述

Great job.

At this time, randomly aligned to a file right-click, you will find more of an option, click on it to get the effective path to the file, directly pasted into the code can be.

在这里插入图片描述

Note: When you click on it, the python window will pop up, and you need to wait for the execution window to close automatically before you can see the copied path on the pasteboard.

ps:python add absolute path with backslash and forward slash difference

# Use absolute paths Double backslashes
with open('E:\\use\\') as file_object:
  contents = file_object.read()
  print(())
  
# Use absolute paths forward slashes
with open('E:/use/') as file_object:
  contents = file_object.read()
  print(())

summarize

to this article about python in the context menu to add a copy of the target file's effective storage path (single slash or double backslash) of the article is introduced to this, more related python context menu storage path content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!