Hi when I comment out a line in Pycharm using the ctrl+slash keyboard shortcut, I get
# code
but instead I want
##Code
like in IDLE.
Is this possible?
You can register a new file type and at the time of registration specify comment characters. You need to reassign *.py to this new type from the default Python file type that comes with installation.
Step by step procedure:
Ctrl+Alt+S to open File Types dialogue.
Alt+Insert (or click on +) to create a new file type
In the new File Type dialogue for Syntax Highlighting -> Line comment: provide ##
Provide rest of the details (including Name and Description) as needed and click OK.
Select the newly created File Type and in Registered Patterns section click + to add a wildcard.
In Add wildcard dialogue box enter *.py and click Ok
Since *.py is already assigned to the default type you will be asked to confirm Reassign wildcard. Do so.
Apply and Ok
Note: We have to do all this because we can not edit default file types and Python is a default file type.
Now, open a Python file (.py extension) and check Ctrl+Slash on any line. You should now see ## prepended to the line.
Ctrl+Slash on any line comments the line i.e. Ctrl + /
Related
When I get a syntax error in Python I think VSCode is supposed to offer a mouse hover over the error message, which lets you Ctrl-click to go to the errored line in the editor.
I'm not getting the hover on the final line in the traceback (but it works OK on earlier lines).
I think it's some kind of configuration problem - but what?
This does work - I get "Open file in editor" (mouse at red arrow):
But this doesn't work (no hover message; mouse at red arrow):
(naturally, per Murphy's Law, the latter is the one I want to work, as the error is in fact on line 2171.)
UPDATE:
The problem is that there are spaces in your file path.
To fix it, change your folder name to make the file path recognizable. Generally, the file path should be in full English, with no spaces and no special symbols.
Hover over the file path to see the underscore including the line number. At this point Ctrl + click will navigate to the corresponding line number of the corresponding file.
I added a new right-click context menu for my own file type (ftype) under HKCU\SOFTWARE\Classes\ftype\shell as do something with ftype and command as "path\ftype.bat" "%1".
I was able to read command-line arguments from ftype.bat when I right click on a .ftype file.
But am facing issues when I select multiple .ftype files and right click to select my command !
For each selected file, my bat is called like,
ftype.bat file1.ftype
ftype.bat file2.ftype
Any ways to make it to work the below way?
ftype.bat file1.ftype file2.ftype
I have tried replacing "%1" in command field with "%*" and with "%1" "%2". But I was able to see spaces being passed to executable !
Note:
Actual regedit entry is a python file and not a bat file as below:
<python_exe_path> "<ftype_handler.py>" "%1"
You can't with a classic static verb using %1. You need to use a drop handler or DelegateExecute shell extension.
This question already has answers here:
How to check type of files without extensions? [duplicate]
(10 answers)
Closed 6 years ago.
Most of the time when we create a new text file with gedit in linux then the file is not saved with an extension of .txt for text file.So how will I recognise it with django code because here I can't check file extension.Here is my code...
Let's say i have a resume field for each user in following models.py
class User(AbstractUser):
resume= models.FileField( upload_to=get_attachment_file_path,default=None, null=True,validators=[validate_file_extension])
Now i want to Validate the file for allowed extension so I made a
validators.py as below
def validate_file_extension(fieldfile_obj):
megabyte_limit = 5.0
filesize = sys.getsizeof(fieldfile_obj)
ext = os.path.splitext(fieldfile_obj.name)[1]
print("extensionnnnnnnnnnnnn",ext)
valid_extensions = ['.pdf', '.doc', '.docx', '.jpg', '.png', '.xlsx', '.xls','.txt','.odt']
if not ext.lower() in valid_extensions:
raise ValidationError(u'Unsupported file extension.')
elif filesize > megabyte_limit*1024*1024:
raise ValidationError("Max file size is %s Byte" % str(megabyte_limit))
Now whenever I upload a text file in my api then it says unsupported file type because the code is unable to get the extension of linux text file.So how can i recognise that text file which is not saved as demo.txt instead my text file is saved as only demo but it is text file as seen from property of that file.
Also my next question is to get the size of each file uploaded in that FileField.I am using PostgreSQL as Dbms
You probably want to detect the upload's MIME type regardless of file extension, and that's often done by reading the file header to detect "magic numbers" or other bit patterns indicating the true nature of a file. Often text files are an edge case, where no header is detected and the first x bytes are printable ASCII or Unicode.
While that's a bit of a rabbit hole to dive into, there's a few Python libraries that will do that for you. For example: https://github.com/ahupp/python-magic will work for your needs by simply inferring the mime type per the file contents, which you will then match against the types you want to accept.
A somewhat related set of example code specific to your needs can be found here: https://stackoverflow.com/a/28306825/7341881
Edit: Eddie's solution is functionality equivalent; python-magic wraps libmagic, which is what Linux's native "file" command taps into. If you do decide to go the subprocess route, do be extra careful you're not creating a security vulnerability by improperly sanitizing user input (eg the user's provided filename). This could lead to an attack granting arbitrary access to your server's runtime environment.
Easy 3 line solution with no external dependencies.
import subprocess
file_info = subprocess.getoutput('file demo')
print(file_info)
In POSIX systems (Linux, Unix, Mac, BSD etc) you can use a file command, for example file demo will display the file info even if the file extension is not explicitly set.
demo is the argument for the file command in other words the actual file you are trying to detect.
Disclaimer, be extra careful running external commands.
Please follow this link for more info about the Python subprocess module.
https://docs.python.org/3.6/library/subprocess.html
I am hosting a text file on OpenShift inside the cron/minutely directory. The server is trying to run the text file every minute, only I don't want it to: all I want the text file to do is stay on the server so I can save useful information inside it.
How do I tell the server that I want it to ignore the text inside the file instead of running it as code? Is there a way to do it by adding a specific "shebang" line at the beginning? Is it even possible to keep the text file in the cron/minutely directory without it being executed?
Thank you.
You can add this as the first line of the text file:
#!/usr/bin/true
That's a sort of fake shebang line. It means that if the file is executed, the "interpreter" to "run" the file is the true program, which is a trivial program which does nothing but return the status code for success (0).
Therefore, a true shebang will make any file "successfully" execute, but do nothing.
Please don't take this to mean that leaving data files in a directory meant for executables is a good idea; it's not. This is just a hack of last resort.
I am trying to port a Sublime Text build system to a plugin.
The build system would receive the current file and go through it with this code:
for line in fileinput.input(inplace=1):
sys.stdout.write(makeReplacements(line))
Now, in the plugin syntax I go the fact that the way to get my current file's content is:
input = self.view.substr(
sublime.Region(0, self.view.size())
)
But now I'm not sure about what I should do about the next operation.
for line in input(inplace=1):
How could I make replacements in the file on-the-fly and then save it?
I don't think the Sublime Text plugin API can save the buffer, but you could use the file_name() method in the sublime.View class and work with the file directly.
As noted by #MattDMo, the file can be saved using view.run_command('save').
It may be easier to use the file name if your old build file worked with that.
As stated by #RazerM, the first argument has to be the file path. So for my example, this will work.
for line in fileinput.input(self.view.file_name(), inplace=1):
sys.stdout.write(self.makeReplacements(line))