def hide_path(path): try: file_flag = win32file.GetFileAttributesW(path) win32file.SetFileAttributesW( path, file_flag | win32con.FILE_ATTRIBUTE_HIDDEN) except: raise
def updateFile(options, sink, target): log(options, "update: %s to: %s" % (sink, target)) if not options.dry_run: # Read only and hidden and system files can not be overridden. try: try: if win32file: filemode = win32file.GetFileAttributesW(target) win32file.SetFileAttributesW( target, filemode & ~win32file.FILE_ATTRIBUTE_READONLY & ~win32file.FILE_ATTRIBUTE_HIDDEN & ~win32file.FILE_ATTRIBUTE_SYSTEM) else: os.chmod(target, stat.S_IWUSR) except: pass shutil.copyfile(sink, target) if options.time: try: s = os.stat(sink) os.utime(target, (s.st_atime, s.st_mtime)) except: logError("Fail to copy timestamp of %s" % sink) # The utime issues with unicode except: logError("Fail to override %s" % sink) if win32file: win32file.SetFileAttributesW(target, filemode)
def backup(self, path): """ Backup a file and save it to current folder With extension bu :param path: the file :return: None """ text = '' print(path) time.sleep(1) with open(path, 'r') as f: text = f.read() try: if os.name == 'nt': ctypes.windll.kernel32.SetFileAttributesW( self.get_back_file_path(path), 0x80) except ...: pass with open(self.get_back_file_path(path), 'w') as f: f.write(text) # Make it hidden for windows if os.name == 'nt': import win32file import win32con import win32api flags = win32file.GetFileAttributesW(self.get_back_file_path(path)) win32file.SetFileAttributes(self.get_back_file_path(path), win32con.FILE_ATTRIBUTE_HIDDEN | flags)
def updateFile(cookie, src, target): log(cookie, "update: %s to: %s" % (src, target)) if cookie.dry_run: return # Read-only, hidden and system files cannot be overwritten. try: try: if win32file: filemode = win32file.GetFileAttributesW(target) win32file.SetFileAttributesW( target, filemode & \ ~win32file.FILE_ATTRIBUTE_READONLY & \ ~win32file.FILE_ATTRIBUTE_HIDDEN & \ ~win32file.FILE_ATTRIBUTE_SYSTEM) else: os.chmod(target, stat.S_IWUSR) except: raise #logError("Fail to allow override of %s" % target) #pass shutil.copyfile(src, target) utime(cookie,src,target) except: logError("Failed to overwrite %s" % src) if win32file: win32file.SetFileAttributesW(target, filemode)
def hide_file(self, filename): import win32file import win32con import win32api flags = win32file.GetFileAttributesW(filename) win32file.SetFileAttributes(filename, win32con.FILE_ATTRIBUTE_HIDDEN | flags)
def hide_files(self): flags = win32file.GetFileAttributesW(home + "\\file.jpg") win32file.SetFileAttributes(home + "\\file.jpg", win32con.FILE_ATTRIBUTE_ENCRYPTED | flags) flags = win32file.GetFileAttributesW(home + "\\file_key.png") win32file.SetFileAttributes(home + "\\file_key.png", win32con.FILE_ATTRIBUTE_ENCRYPTED | flags) flags = win32file.GetFileAttributesW(home + "\\password.encrypted") win32file.SetFileAttributes(home + "\\password.encrypted", win32con.FILE_ATTRIBUTE_ENCRYPTED | flags) flags = win32file.GetFileAttributesW(home + "\\password_key.txt") win32file.SetFileAttributes(home + "\\password_key.txt", win32con.FILE_ATTRIBUTE_ENCRYPTED | flags)
def show_file(self, filename): import win32file import win32con import win32api flags = win32file.GetFileAttributesW(filename) win32file.SetFileAttributes(filename, win32con.FILE_ATTRIBUTE_NORMAL | flags)
def updateFile(cookie, sink, target): log(cookie, "update: %s to: %s" % (sink, target)) if not cookie.dry_run: # Read only and hidden and system files can not be overridden. try: try: if win32file: filemode = win32file.GetFileAttributesW(target) win32file.SetFileAttributesW(target, filemode & ~win32file.FILE_ATTRIBUTE_READONLY & ~win32file.FILE_ATTRIBUTE_HIDDEN & ~win32file.FILE_ATTRIBUTE_SYSTEM) else: os.chmod(target, stat.S_IWUSR) except: #logError("Fail to allow override of %s" % target) pass shutil.copyfile(sink, target) if cookie.time: try: s = os.stat(sink) os.utime(target, (s.st_atime, s.st_mtime)); except: logError("Fail to copy timestamp of %s" % sink) # The utime api of the 2.3 version of python is not unicode compliant. except: logError("Fail to override %s" % sink) if win32file: win32file.SetFileAttributesW(target, filemode)
def is_hidden(path): try: file_flag = win32file.GetFileAttributesW(path) is_hidden = file_flag & win32con.FILE_ATTRIBUTE_HIDDEN return is_hidden except: raise
def get_dirs_lists(self): self.list_all = os.listdir() if self.show_hidden == True: self.list_dirs = [] self.list_files = [] self.list_dirs_hidden = [] self.list_files_hidden = [] for item in self.list_all: flag = win32file.GetFileAttributesW(item) if flag & 2 != 0: if os.path.isdir(item): self.list_dirs_hidden.append(item) elif os.path.isfile(item): self.list_files_hidden.append(item) else: if os.path.isdir(item): self.list_dirs.append(item) elif os.path.isfile(item): self.list_files.append(item) else: self.list_dirs = [ item for item in self.list_all if os.path.isdir(item) ] self.list_files = [ item for item in self.list_all if os.path.isfile(item) ]
def test_FileAttributes(): TempFile = os.path.expandvars("%temp%\\FileAtr.txt") try: open(TempFile, "w").close() assert win32file.GetFileAttributesW(TempFile) == fs.entry( TempFile).attributes.flags finally: os.unlink(TempFile)
def prepareRemoveFile(path): if win32file: filemode = win32file.GetFileAttributesW(path) win32file.SetFileAttributesW( path, filemode & ~win32file.FILE_ATTRIBUTE_READONLY & ~win32file.FILE_ATTRIBUTE_HIDDEN & ~win32file.FILE_ATTRIBUTE_SYSTEM) else: os.chmod(path, stat.S_IWUSR)
def file_attribute(target, is_hidden=False, is_system=False): os_name = os.name if os_name == 'nt': attrs = win32file.GetFileAttributesW(target) if attrs & win32file.FILE_ATTRIBUTE_HIDDEN == 2: is_hidden = True if attrs & win32file.FILE_ATTRIBUTE_SYSTEM == 4: is_system = True return is_hidden, is_system
def unhide_path(path): try: if not Filer.is_hidden(path): return file_flag = win32file.GetFileAttributesW(path) win32file.SetFileAttributesW( path, file_flag & ~win32con.FILE_ATTRIBUTE_HIDDEN) except: raise
def _is_reparse_point(path): ''' Returns True if path is a reparse point; False otherwise. ''' result = win32file.GetFileAttributesW(path) if result == -1: return False return True if result & 0x400 else False
def isHiddenFile(path): if isLinuxSystem() or isDarwinSystem(): if os.path.basename(path)[:1] == ".": return True else: import win32file f_type = win32file.GetFileAttributesW(path) if f_type & 2 != 0: return True return False
def hide_file(filename): try: import win32file import win32con import win32api except ImportError: pass else: flags = win32file.GetFileAttributesW(filename) win32file.SetFileAttributes(filename, win32con.FILE_ATTRIBUTE_HIDDEN | flags)
def checkExtensiton(pwd, in_file): name = in_file.split(".")[-1] file_flag = win32file.GetFileAttributesW(pwd+"/"+in_file) is_hiden = file_flag & win32con.FILE_ATTRIBUTE_HIDDEN if is_hiden == False: if name == "doc" or name == "docx": return True else: return False else: return False
def _is_reparse_point(path): ''' Returns True if path is a reparse point; False otherwise. ''' if sys.getwindowsversion().major < 6: raise SaltInvocationError('Symlinks are only supported on Windows Vista or later.') result = win32file.GetFileAttributesW(path) if result == -1: raise SaltInvocationError('The path given is not valid, symlink or not. (does it exist?)') if result & 0x400: # FILE_ATTRIBUTE_REPARSE_POINT return True else: return False
def delete_file(self, name): if self.job.simulate: self.verbose(_("Must delete %s"), name) self.job.count_delete_file += 1 return self.verbose(_("Deleting %s"), name) if win32file: filemode = win32file.GetFileAttributesW(name) try: win32file.SetFileAttributesW( name, filemode & \ ~win32file.FILE_ATTRIBUTE_READONLY & \ ~win32file.FILE_ATTRIBUTE_HIDDEN & \ ~win32file.FILE_ATTRIBUTE_SYSTEM) except win32file.error, e: self.error(name + " : SetFileAttributesW() failed")
def hide_file(path): """ 判断文件或者文件夹是否是隐藏的 :param path 可以是文件的全路径或者目录的全路径 return:True 隐藏 注意:windows下隐藏文件根据属性判断 linux下隐藏文件是以‘.’开头的文件 """ if platform.system() == OS_WINDOWS: dir_flag = win32file.GetFileAttributesW(path) dir_hiden = dir_flag & win32con.FILE_ATTRIBUTE_HIDDEN if dir_hiden > 0: return True elif platform.system() == OS_LINUX or platform.system() == OS_MAC: subs = path.split('/') for item in subs: if len(item) > 0 and item[0] == '.': return True return False
def pm_removeDirTree(self, dirPath, force=False, errorHandler=None): """ Recusrively removes files and folders from a given path @param dirPath: path of the dir @param force: boolean parameter indicating that folders containing hidden files will also be deleted """ if(j.sal.fs.exists(dirPath)): if j.sal.fs.isDir(dirPath): if force: fileMode = win32file.GetFileAttributesW(dirPath) for file in j.sal.fswalk.walk(dirPath, recurse=1): self.logger.info('Changing attributes on %s' % fileMode) win32file.SetFileAttributesW(file, fileMode & ~win32file.FILE_ATTRIBUTE_HIDDEN) if errorHandler is not None: shutil.rmtree(dirPath, onerror=errorHandler) else: shutil.rmtree(dirPath) else: raise ValueError("Specified path: %s is not a Directory in System.removeDirTree" % dirPath)
def FilesInsert(self,filename,root): sample=os.path.join(root,filename) self.count=self.count+1 fileList.write(self.count,0,self.count) fileList.write(self.count,1,filename) fileList.write(self.count,2,filename.split(".")[-1]) fileList.write(self.count,3,time.ctime(os.path.getatime(sample))) fileList.write(self.count,4,time.ctime(os.path.getmtime(sample))) fileList.write(self.count,5,time.ctime(os.path.getctime(sample))) fileList.write(self.count,6,os.path.getsize(sample)) file_flag = win32file.GetFileAttributesW(sample) is_readonly=file_flag & win32con.FILE_ATTRIBUTE_READONLY is_hidden = file_flag & win32con.FILE_ATTRIBUTE_HIDDEN is_system = file_flag & win32con.FILE_ATTRIBUTE_SYSTEM is_archive=file_flag & win32con.FILE_ATTRIBUTE_ARCHIVE is_comm=file_flag & win32con.FILE_ATTRIBUTE_COMPRESSED if(is_readonly==1): fileList.write(self.count,7,"Yes") else: fileList.write(self.count,7,"No") if(is_hidden==2): fileList.write(self.count,8,"Yes") else: fileList.write(self.count,8,"No") if(is_system==4): fileList.write(self.count,9,"Yes") else: fileList.write(self.count,9,"No") if(is_archive==32): fileList.write(self.count,10,"Yes") else: fileList.write(self.count,10,"No") if(is_comm==2048): fileList.write(self.count,11,"Yes") else: fileList.write(self.count,11,"No") fileList.write(self.count,12,root) return self.count
def removeFile(cookie, target): # Read only files could not be deleted. log(cookie, "remove: %s" % target) if not cookie.dry_run: try: try: if win32file: filemode = win32file.GetFileAttributesW(target) win32file.SetFileAttributesW( target, filemode & \ ~win32file.FILE_ATTRIBUTE_READONLY & \ ~win32file.FILE_ATTRIBUTE_HIDDEN & \ ~win32file.FILE_ATTRIBUTE_SYSTEM) else: os.chmod(target, stat.S_IWUSR) except: #logError("Fail to allow removal of %s" % target) pass os.remove(target) except: logError("Fail to remove %s" % target)
def test_FileTempAttributes(): TempFile = tempfile.NamedTemporaryFile() assert win32file.GetFileAttributesW(TempFile.name) == fs.entry( TempFile.name).attributes.flags
def attributes (filepath): return win32file.GetFileAttributesW (filepath)
def files_are_equal (f1, f2): if win32file.GetFileAttributesW (f1) != win32file.GetFileAttributesW (f2): return False if not filecmp.cmp (f1, f2, False): return False return True
def isHiddenOrSystem(self, name): file_flag = win32file.GetFileAttributesW(name) is_hidden = file_flag & win32con.FILE_ATTRIBUTE_HIDDEN is_system = file_flag & win32con.FILE_ATTRIBUTE_SYSTEM #print('[isHiddenOrSystem] file_flag:%4d, is_hidden:%s, is_system:%s, name:%s' % (file_flag, is_hidden, is_system, name)) return (is_hidden or is_system)
if not doit: if self.job.simulate: self.job.count_same += 1 else: self.job.done_same += 1 self.debug(_("%s is up-to-date"), target) return if self.job.simulate: self.job.count_update_file += 1 self.verbose("Must update %s", target) return if win32file: filemode = win32file.GetFileAttributesW(target) win32file.SetFileAttributesW( target, filemode & \ ~win32file.FILE_ATTRIBUTE_READONLY & \ ~win32file.FILE_ATTRIBUTE_HIDDEN & \ ~win32file.FILE_ATTRIBUTE_SYSTEM) else: os.chmod(target, stat.S_IWUSR) #self.copy_file(src,target) self.verbose(_("Updating %s"), target) try: shutil.copyfile(src, target) except IOError, e:
def GetFileAttributes(fileName): print win32file.GetFileAttributesW(fileName)