To manipulate a file in Python:
1. First open the file, get the file handle and assign a value to a variable.
2. Manipulate the file.
3. Close the file.
Let's start by opening a file, the general variable is denoted by F
f = open (' filename ', ' Open file Mode ', encoding = ' encoded format ')
F.readline () reads a line of content
F.read () reads the contents of the entire file
F.tell () Where the current file is read
F.seek () Recalls where the file is read
F.fileno () returns the label of the file handle
F.isatty () is not an end device
F.wirte ()
Mode of open File
R: Read-only mode.
W: Write-only mode non-readable does not exist on the creation of existing on the delete content
A: Append mode. Create a readable non-existent: Append content exists
r+: Readable writable file
w+: Write a file.
A +: Same as a mode
"U" means that you can convert \ r \ n \ r \ NAND \ n automatically when reading
RU or R+u
b means processing a binary file
RB Read binary file
WB Write Binary file
AB Append binary file
To add, we can also use the with open (' filename ', ' Open file Mode ', encoding = ' encoded format ') as F for opening the file so we don't have to write F. Close ()
These are the file operations in Python, so let's take a look at the operations of the files in OC.
iOS is a sandbox mechanism in which an app can access only the contents of files in the current app directory, with three folders in each sandbox
Documents: Common directory, icloud backup directory, storage data, cannot save cache files, otherwise on the shelves do not pass.
Library: There are two files under this file caches: storing large volumes of data that do not need to be backed up, such as music, video, Sdwebimage caching.
Preference: Store some preferences.
TMP: Temporary files are not backed up, and the data under this file may be deleted at any time.
Get the following path to the sandbox:
NSString *direntoryhome = Nshomedirectory ();
Get Documents Directory File
Nsarray *path = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *documentsdir = [path objectatindex:0];
Get Library Directory files
Nsarray *path = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES);
NSString *documentsdir = [path objectatindex:0];
Get the Cache directory file
Nsarray *path = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);
NSString *documentsdir = [path objectatindex:0];
Get the TMP directory path
NSString *string = Nstemporarydirectory ();
Create a folder
Nsarray *path =nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES);
NSString *documentsdirectory = [path objectatindex:0];
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
NSString *textdirectory = [documentsdirectory stringbyappendingpathcomponent:@ "test"];
BOOL res = [FileManager createdirectoryatpath:textdirectory withintermediatedirectories:yes Attributes:nil Error:nil] ;
Create a file
[FileManager Createfileatpath:testpath Contents:nil Attributes:nil];
Delete a file
[FileManager Removeitematpath:testpath Error:nil];
Check if a file exists
[FileManager Fileexistsatpath:testpath];
Check if a file is readable
[FileManager Isreadablefileatpath:testpath];
Writing data to a file
[@ "" Writetofile:testpath atomically:yes encoding:nsutf8stringencoding Error:nil];
Read the contents of a file
[NSString Stringwithcontentsoffile:testpath encoding:nsutf8stringencoding Error:nil];
The movement of a file or directory
[Manager Moveitematpath:filepath Topath:filepath2 Error:nil]
Copying of files
[Manager copyitematpath:filepath2 Topath:filepath3 Error:nil]
Management of files in Python and file management in OC