Differentiating between file extension and hidden files beginning with a .dot - python

The file extension is typically everything after the last period. If a filename has no ".", it has no extension. What happens when the filename begins with a dot, as hidden files in linux do?
In python, the file has no extension...
>>> os.path.splitext("base.ext")
('base', '.ext')
>>> os.path.splitext(".ext")
('.ext', '')
The common method in bash produces the other result where there is only an extension and no base part (Extract filename and extension in Bash)...
>>> filename=".ext"
>>> extension="${filename##*.}"
>>> base="${filename%.*}"
>>> echo $base
>>> echo $extension
ext
How should code handle filenames such as this? Is there a standard? Does it differ per operating system? Or simply which is most common/consistent?
[EDIT]
Lets say you have a file that's just ".pdf". Should, for example, an open dialogue default to listing it without 1. showing hidden files and 2. allowing all file extensions?
It's a hidden file - it begins with a period
Is it actually a .pdf (by filename convention, sure it has pdf data) or is it a file witn no extension?

File extensions in POSIX-based operating systems have no innate meaning; they're just a convention. Changing the extension wouldn't change anything about the file itself, just the name used to refer to it.
A file could have multiple extensions:
source.tar.gz
Sometimes a single extension represents a contraction of two:
source.tgz
Other files may not have an extension at all:
.bashrc
README
ABOUT
TODO
Typically, the only thing that defines an extension is that it is a trailing component of a filename that follows a non-initial period. Meaning is assigned by the application examining the file name. A PDF reader may focus on files whose names end with .pdf, but it should not refuse to open a valid PDF file whose name does not.
Note that
extension="${filename##*.}"
is simply an application of a parameter expansion operator which only returns the (final) extension if the filename does not start with a period. It's not an extension operator, it is a prefix-removal operator.

Related

Opening files in python3 for windows10

Hi I cannot open files in python 3 actually I have a problem with the path. I don't know how to write the path for it.:/ For example I have a file(bazi.py) in folder(w8) in driver(F). How should i write it's path. Please help me im an amateur:/
In Windows, there are a couple additional ways of referencing a file. That is because natively, Windows file path employs the backslash "" instead of the slash. Python allows using both in a Windows system, but there are a couple of pitfalls to watch out for. To sum them up:
Python lets you use OS-X/Linux style slashes "/" even in Windows. Therefore, you can refer to the file as 'C:/Users/narae/Desktop/alice.txt'. RECOMMENDED.
If using backslash, because it is a special character in Python, you must remember to escape every instance: 'C:\Users\narae\Desktop\alice.txt'
Alternatively, you can prefix the entire file name string with the rawstring marker "r": r'C:\Users\narae\Desktop\alice.txt'. That way, everything in the string is interpreted as a literal character, and you don't have to escape every backslash.
File Name Shortcuts and CWD (Current Working Directory)
So, using the full directory path and file name always works; you should be using this method. However, you might have seen files called by their name only, e.g., 'alice.txt' in Python. How is it done?
The concept of Current Working Directory (CWD) is crucial here. You can think of it as the folder your Python is operating inside at the moment. So far we have been using the absolute path, which begins from the topmost directory. But if your file reference does not start from the top (e.g., 'alice.txt', 'ling1330/alice.txt'), Python assumes that it starts in the CWD (a "relative path").
using the os.path.abspath function will translate the path to a version appropriate for the operating system.
os.path.abspath(r'F:\w8\bazi.py')

SSH2 Python How To Recognize When File Is Directory

I am converting my python program from paramiko to ssh2. I have succeeded in authenticating and I can get a directory listing. Where I am stuck is as I process through the directory listing how do I recognize whether the "file" is a directory or a file. I see the attributes but of those I can only see atime being something I will want to use (to know how old the file is). Once I have done the opendir and readdir (and so have a listing of files) how do I recognize whether each is a file or a directory?
When I do the readdir I am returned:
Length of filename
filename
attributes
atime
filesize
flags
gid
mtime
permissions
uid
Haven't used ssh2-python myself but I would say to check the contents of flags. According to the library's documentation (as suggested by #NullPointerException) the possible values are:
LIBSSH2_SFTP_S_IFMT
Type of file mask
LIBSSH2_SFTP_S_IFIFO
Named pipe (fifo)
LIBSSH2_SFTP_S_IFCHR
Character special (character device)
LIBSSH2_SFTP_S_IFDIR
Directory
LIBSSH2_SFTP_S_IFBLK
Block special (block device)
LIBSSH2_SFTP_S_IFREG
Regular file
LIBSSH2_SFTP_S_IFLNK
Symbolic link
LIBSSH2_SFTP_S_IFSOCK
Socket
I would say flags is a bit field and you have to check if certain flag is "on" or not with a bitwise operator, for example, to check it it's a directory:
flags & LIBSSH2_SFTP_S_IFDIR == LIBSSH2_SFTP_S_IFDIR

isfile not recognising files python 2.5

I have the following code:
with open('EcoDocs TK pdfs.csv', 'rb') as pdf_in:
pdflist = csv.reader(pdf_in, quotechar='"')
for row in pdflist:
if row[1].endswith(row[2]):#check if file type is appended to file name
pathname = ''.join(row[0:2])
else:
pathname = ''.join(row)
if os.path.isfile(pathname):
filehash = md5.md5(file(pathname).read()).hexdigest()
It reads in file paths, file names and file types from a csv file. It then checks to see if the file type is appended to the file name, before joining the file path and file name. It then checks to see if the file exists, before doing something with the file. There are about 5000 file names in the csv file, but isfile only returns True for about half of these. I've manually checked that some of those isfile returns False for exist. As all the data is read in, there shouldn't be any problems with escape characters or single backslashes, so I'm a bit stumped. Any ideas? An example of the csv file format is below, as well as an example of some of the pathnamethat isfile can't find.
csv file-
c:\2dir\a. dir\d dir\lo dir\fu dir\wdir\5dir\,5_l B.xls,.xls
c:\2dir\a. dir\d dir\lo dir\fu dir\wdir\5dir\,5_l A.pdf,.pdf
pathname created-
c:\2dir\a. dir\d dir\lo dir\fu dir\wdir\5dir\5_l B.xls
c:\2dir\a. dir\d dir\lo dir\fu dir\wdir\5dir\5_l A.pdf
Thanks.
You can safely assume that os.path.isfile() works correctly. Here is my process to debug issues like this:
Add a print(pathname) before I use it.
Eyeball the output. Does anything look suspicious?
Copy the output into the clipboard -> Win+RcmdReturndirSpace" + paste into new command prompt + "Return
That checks whether the path is really correct (finds slight mistakes that eyeballing will miss). It also helps to validate the insane DOS naming conventions which are still enforced even on Windows.
if this also works, the next step is to check file and folder permissions: Make sure the user that runs the script actually has permissions to see and read the file.
EDIT Paths on Windows are ... complicated. An important detail, for example, is that "." is a very, very special character. The name "a.something very long" isn't valid in the command prompt because it demands that you have at most three characters after the last "." in a file name! You're just lucky that it doesn't demand that the name before the last dot is at most 8 characters.
Conclusion: You must be very, very, very careful with "strange characters" in file names and paths on Windows. The only characters which are safe are listed in this document.

Python: A way to detect a filetype attached to a string?

In IronPython 2.6*, I'm trying to build a function that "corrects" a string; I have two arguments, FILE and EXTN. The idea is for them to be concatenated as necessary later in the program, but you know some people don't read instructions and you're bound to have someone enter "FILE.*" as their FILE, which would mess everything up.
I'm looking for a way to take FILE, have my function detect and strip .* (any extension of any length) from FILE if .* exists; It doesn't need to be in the string, and the user will be entering the same extension into EXTN**, so it needs not be prepared, merely consistently stripped.
My current method has me passing FILE and EXTN separately, but it's not inconceivable to redo things to take FILE.EXTN and break that into FILE and EXTN if need be; I don't want to if I don't have to, though, as my program is built around the former system.
*A note regarding IronPython 2.6; I'm trying to avoid IronPython-specific codes and use as simple of ones as possible, for UNIX-WIN cross-compatibility's sake. So far, everything I've done works in Python 2.7 IDE's, but obviously will not work in Python 3.x
**A note regaring EXTN; I want users to enter the proper extension into EXTN too, but as we know, we can't be sure of this and so the method for stripping .i from FILE must not automatically include EXTN as part of it.
Here is a snippet of code that may help as a reference for what I have so far. The FILE and EXTN variables have been added, and in practice, are pulled in through a middle-man program from an XML file into the script at run-time.
FILE = "test"
PATH = "C:\\"
EXTN = ".txt"
def CheckCorrect_FILE(srcFile): #Check-corrects FILE
#Meh, I got nothin'...
def CheckCorrect_PATH(srcPath): #Check-corrects PATH
if srcPath.endswith('\\') == False:
srcPath = srcPath + "\\"
else:
srcPath = srcPath
return srcPath
You can do this using os.path.splitext. The following will always remove an extension if one exists (and do nothing if it doesn't):
import os
FILE = os.path.splitext(FILE)[0]

How to write a whole string to a file in Python and where does the file go?

I am doing an assignment on text formatting and alignment (text wrapping) and I need to write my formatted string to new file. But once I have written to the file (or think I've written) where does that file go? Does it actually create a file on my desktop or am I being stupid?
This is my code:
txtFile = open("Output.txt", "w")
txtFile.write(string)
txtFile.close()
return txtFile
Cheers,
JT
The text is written to a file called "Output.txt" in your working directory (which is usually the directory from which the script has been executed).
To display the working directory, you can use:
>>> import os
>>> os.getcwd()
'/home/adam'
When you open a file without specifying a file path, the file will be created in the python scripts working directory.
Usually that is the location of your script but there are times when it may be a different place.
The os module in python will provide functions for checking and changing the working directory within python itself.
most notably:
os.chdir(path)
os.fchdir(fd)
os.getcwd()
It will create a new file called "Output.txt" in the same directory that you executed your script from. It may mean that the file can't be written to, if you're in a directory that doesn't have the appropriate permissions for your user.

Categories

Resources